Skip to content

Commit

Permalink
Merge pull request #30623 from geoand/#30608
Browse files Browse the repository at this point in the history
Introduce convertScopes to `@InjectSpy`
  • Loading branch information
geoand authored Jan 26, 2023
2 parents 87769af + d309d9c commit aad2385
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 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 @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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,22 +26,26 @@
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) {
return new Consumer<BuildChainBuilder>() {
return new Consumer<>() {

@Override
public void accept(BuildChainBuilder buildChainBuilder) {
buildChainBuilder.addBuildStep(new BuildStep() {
@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 aad2385

Please sign in to comment.