Skip to content

Commit

Permalink
Implement review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Sep 6, 2024
1 parent 96480e8 commit 99fc3a8
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 32 deletions.
6 changes: 6 additions & 0 deletions docs/extensions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,12 @@ Another problematic case is when the type is `final` or `sealed` as those are no
To address these issues, Spock 2.4 introduces the `org.spockframework.runtime.extension.IDefaultValueProviderExtension` that is loaded via Java's `ServiceLoader` mechanism.
This extension allows you to provide a default value if the `EmptyOrDummyResponse` doesn't have specific instructions for a type.

.Example Implementation
[source,groovy,indent=0]
----
include::{sourcedir-java}/smoke/mock/MaybeDefaultValueProvider.java[tag=sample-implementation]
----

It is primarily for framework developers who want to provide a default value for their framework types.
Or users of a framework that doesn't provide default values for their special types.

Expand Down
4 changes: 4 additions & 0 deletions docs/include.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
:github-base: https://github.com/spockframework/spock
:github-blob-base: {github-base}/blob
:commit-ish: master
// source java dir
:base-sourcedir-java: spock-specs/src/test/java/org/spockframework
:sourcedir-java: ../{base-sourcedir-java}
:github-sourcedir-java: {github-blob-base}/{commit-ish}/{base-sourcedir-java}
// source dir
:base-sourcedir: spock-specs/src/test/groovy/org/spockframework/docs
:sourcedir: ../{base-sourcedir}
Expand Down
1 change: 1 addition & 0 deletions docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include::include.adoc[]
=== Misc

* Add `globalTimeout` to `@Timeout` extension, to apply a timeout to all features in a specification, configurable via the spock configuration file spockPull:1986[]
* Add new <<extensions.adoc#default-value-provider,`IDefaultValueProviderExtension`>> extension point to add support for special classes in the Stub's default `EmptyOrDummyResponse` spockPull:1994[]
* Improve `@Timeout` extension will now use virtual threads if available spockPull:1986[]
* Improve mock argument matching, types constraints or arguments in interactions can now handle primitive types like `_ as int` spockIssue:1974[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private EmptyOrDummyResponse() {
for (IDefaultValueProviderExtension provider : serviceLoader) {
providers.add(provider);
}
providers.sort(Comparator.comparing(p -> p.getClass().getName()));
defaultValueProviders = Collections.unmodifiableList(providers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
public interface IDefaultValueProviderExtension {
/**
* Provides a default value for the given type.
* <p>
* This method will be called for every `EmptyOrDummyResponse` non-default type, the returned values are not cached.
*
* @param type the type for which a default value should be provided, see {@link IMockMethod#getReturnType()}
* @param exactType the exact type for which a default value should be provided, see {@link IMockMethod#getExactReturnType()}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.spockframework.runtime.extension.IDefaultValueProviderExtension;
import org.spockframework.util.Nullable;

// tag::sample-implementation[]
public class MaybeDefaultValueProvider implements IDefaultValueProviderExtension {
@Override
public @Nullable Object provideDefaultValue(Class<?> type, Type exactType) {
Expand All @@ -28,3 +29,4 @@ public class MaybeDefaultValueProvider implements IDefaultValueProviderExtension
return null;
}
}
// end::sample-implementation[]

0 comments on commit 99fc3a8

Please sign in to comment.