From 915a4d3f0b4de024b95c732f60b708a641de8e96 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 26 Jan 2023 08:46:42 +0200 Subject: [PATCH 1/2] Introduce convertScopes to @InjectSpy The same way we support convertScopes on @InjectMock we can support it on @InjectSpy as well. Closes: #30608 --- .../java/io/quarkus/it/mockbean/CapitalizerService.java | 4 ++-- .../test/java/io/quarkus/it/mockbean/WithSpiesTest.java | 2 +- .../java/io/quarkus/test/junit/mockito/InjectSpy.java | 8 ++++++++ ...ApplicationScopedTestBuildChainCustomizerProducer.java | 7 ++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/integration-tests/injectmock/src/main/java/io/quarkus/it/mockbean/CapitalizerService.java b/integration-tests/injectmock/src/main/java/io/quarkus/it/mockbean/CapitalizerService.java index 850e7f7df1722..70fbe8edf55b9 100644 --- a/integration-tests/injectmock/src/main/java/io/quarkus/it/mockbean/CapitalizerService.java +++ b/integration-tests/injectmock/src/main/java/io/quarkus/it/mockbean/CapitalizerService.java @@ -1,8 +1,8 @@ package io.quarkus.it.mockbean; -import javax.enterprise.context.ApplicationScoped; +import javax.inject.Singleton; -@ApplicationScoped +@Singleton public class CapitalizerService { public String capitalize(String input) { diff --git a/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java b/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java index 0fde29c32f6dd..135213378ab4f 100644 --- a/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java +++ b/integration-tests/injectmock/src/test/java/io/quarkus/it/mockbean/WithSpiesTest.java @@ -15,7 +15,7 @@ @QuarkusTest class WithSpiesTest { - @InjectSpy + @InjectSpy(convertScopes = true) CapitalizerService capitalizerService; @InjectSpy diff --git a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectSpy.java b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectSpy.java index 0bad87a8a2666..1f1f9a6673688 100644 --- a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectSpy.java +++ b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectSpy.java @@ -23,4 +23,12 @@ * @see org.mockito.AdditionalAnswers#delegatesTo(Object) */ boolean delegate() default false; + + /** + * If true, then Quarkus will change the scope of the target {@code Singleton} bean to {@code ApplicationScoped}. + * This is an advanced setting and should only be used if you don't rely on the differences between {@code Singleton} + * and {@code ApplicationScoped} beans (for example it is invalid to read fields of {@code ApplicationScoped} beans + * as a proxy stands in place of the actual implementation) + */ + boolean convertScopes() default false; } diff --git a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java index a6f35bf089977..d9dbea6c994f5 100644 --- a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java +++ b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java @@ -1,5 +1,6 @@ package io.quarkus.test.junit.mockito.internal; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -25,10 +26,12 @@ import io.quarkus.builder.BuildStep; import io.quarkus.test.junit.buildchain.TestBuildChainCustomizerProducer; import io.quarkus.test.junit.mockito.InjectMock; +import io.quarkus.test.junit.mockito.InjectSpy; public class SingletonToApplicationScopedTestBuildChainCustomizerProducer implements TestBuildChainCustomizerProducer { private static final DotName INJECT_MOCK = DotName.createSimple(InjectMock.class.getName()); + private static final DotName INJECT_SPY = DotName.createSimple(InjectSpy.class.getName()); @Override public Consumer produce(Index testClassesIndex) { @@ -40,7 +43,9 @@ public void accept(BuildChainBuilder buildChainBuilder) { @Override public void execute(BuildContext context) { Set mockTypes = new HashSet<>(); - List instances = testClassesIndex.getAnnotations(INJECT_MOCK); + List instances = new ArrayList<>(); + instances.addAll(testClassesIndex.getAnnotations(INJECT_MOCK)); + instances.addAll(testClassesIndex.getAnnotations(INJECT_SPY)); for (AnnotationInstance instance : instances) { if (instance.target().kind() != AnnotationTarget.Kind.FIELD) { continue; From d309d9cb78109ca49e2a1ce006164f375a8f962a Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 26 Jan 2023 08:48:53 +0200 Subject: [PATCH 2/2] Add minor polish to mockito related code --- .../src/main/java/io/quarkus/test/junit/mockito/InjectMock.java | 2 +- ...etonToApplicationScopedTestBuildChainCustomizerProducer.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectMock.java b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectMock.java index 82aad0650c8bc..d5d71839015cd 100644 --- a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectMock.java +++ b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/InjectMock.java @@ -15,7 +15,7 @@ /** * If true, then Quarkus will change the scope of the target {@code Singleton} bean to {@code ApplicationScoped} - * to make the mockable. + * to make it mockable. * This is an advanced setting and should only be used if you don't rely on the differences between {@code Singleton} * and {@code ApplicationScoped} beans (for example it is invalid to read fields of {@code ApplicationScoped} beans * as a proxy stands in place of the actual implementation) diff --git a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java index d9dbea6c994f5..9906fc368dec3 100644 --- a/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java +++ b/test-framework/junit5-mockito/src/main/java/io/quarkus/test/junit/mockito/internal/SingletonToApplicationScopedTestBuildChainCustomizerProducer.java @@ -35,7 +35,7 @@ public class SingletonToApplicationScopedTestBuildChainCustomizerProducer implem @Override public Consumer produce(Index testClassesIndex) { - return new Consumer() { + return new Consumer<>() { @Override public void accept(BuildChainBuilder buildChainBuilder) {