Skip to content

Commit

Permalink
Introduce convertScopes to @InjectSpy
Browse files Browse the repository at this point in the history
The same way we support convertScopes on @InjectMock
we can support it on @InjectSpy as well.

Closes: quarkusio#30608
  • Loading branch information
geoand committed Jan 26, 2023
1 parent 87769af commit 742a42c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@QuarkusTest
class WithSpiesTest {

@InjectSpy
@InjectSpy(convertScopes = true)
CapitalizerService capitalizerService;

@InjectSpy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@
* @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}
* to make the 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)
*/
boolean convertScopes() default false;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<BuildChainBuilder> produce(Index testClassesIndex) {
Expand All @@ -40,7 +43,9 @@ public void accept(BuildChainBuilder buildChainBuilder) {
@Override
public void execute(BuildContext context) {
Set<DotName> mockTypes = new HashSet<>();
List<AnnotationInstance> instances = testClassesIndex.getAnnotations(INJECT_MOCK);
List<AnnotationInstance> 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;
Expand Down

0 comments on commit 742a42c

Please sign in to comment.