diff --git a/docs/extensions.adoc b/docs/extensions.adoc index ed6baada6e..223c45f625 100644 --- a/docs/extensions.adoc +++ b/docs/extensions.adoc @@ -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. diff --git a/docs/include.adoc b/docs/include.adoc index 54ddcb052c..713bcaf665 100644 --- a/docs/include.adoc +++ b/docs/include.adoc @@ -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} diff --git a/docs/release_notes.adoc b/docs/release_notes.adoc index 47def0a044..030881d615 100644 --- a/docs/release_notes.adoc +++ b/docs/release_notes.adoc @@ -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 <> 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[] diff --git a/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java b/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java index 2cafd75a29..b788ef3b08 100644 --- a/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java +++ b/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java @@ -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); } diff --git a/spock-core/src/main/java/org/spockframework/runtime/extension/IDefaultValueProviderExtension.java b/spock-core/src/main/java/org/spockframework/runtime/extension/IDefaultValueProviderExtension.java index c1db841559..f909e80b9b 100644 --- a/spock-core/src/main/java/org/spockframework/runtime/extension/IDefaultValueProviderExtension.java +++ b/spock-core/src/main/java/org/spockframework/runtime/extension/IDefaultValueProviderExtension.java @@ -41,6 +41,8 @@ public interface IDefaultValueProviderExtension { /** * Provides a default value for the given type. + *

+ * 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()} diff --git a/spock-specs/src/test-java-le-11/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java b/spock-specs/src/test-java-le-11/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java deleted file mode 100644 index 92079e3193..0000000000 --- a/spock-specs/src/test-java-le-11/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * https://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.spockframework.smoke.mock; - -import java.lang.reflect.Type; - -import org.spockframework.runtime.extension.IDefaultValueProviderExtension; -import org.spockframework.util.Nullable; - -public class MaybeDefaultValueProvider implements IDefaultValueProviderExtension { - @Override - public @Nullable Object provideDefaultValue(Class type, Type exactType) { - if (type == IMaybe.class) { - return IMaybe.none(); - } - return null; - } -} diff --git a/spock-specs/src/test-java-le-11/resources/META-INF/services/org.spockframework.runtime.extension.IDefaultValueProviderExtension b/spock-specs/src/test-java-le-11/resources/META-INF/services/org.spockframework.runtime.extension.IDefaultValueProviderExtension deleted file mode 100644 index 39f8db431f..0000000000 --- a/spock-specs/src/test-java-le-11/resources/META-INF/services/org.spockframework.runtime.extension.IDefaultValueProviderExtension +++ /dev/null @@ -1,2 +0,0 @@ -org.spockframework.mock.FinalClassDefaultValueProvider -org.spockframework.smoke.mock.MaybeDefaultValueProvider diff --git a/spock-specs/src/test-java-ge-17/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java b/spock-specs/src/test/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java similarity index 94% rename from spock-specs/src/test-java-ge-17/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java rename to spock-specs/src/test/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java index 92079e3193..482add42d9 100644 --- a/spock-specs/src/test-java-ge-17/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java +++ b/spock-specs/src/test/java/org/spockframework/smoke/mock/MaybeDefaultValueProvider.java @@ -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) { @@ -28,3 +29,4 @@ public class MaybeDefaultValueProvider implements IDefaultValueProviderExtension return null; } } +// end::sample-implementation[] diff --git a/spock-specs/src/test-java-ge-17/resources/META-INF/services/org.spockframework.runtime.extension.IDefaultValueProviderExtension b/spock-specs/src/test/resources/META-INF/services/org.spockframework.runtime.extension.IDefaultValueProviderExtension similarity index 100% rename from spock-specs/src/test-java-ge-17/resources/META-INF/services/org.spockframework.runtime.extension.IDefaultValueProviderExtension rename to spock-specs/src/test/resources/META-INF/services/org.spockframework.runtime.extension.IDefaultValueProviderExtension