Skip to content

Commit

Permalink
Testing: fix @MockitoConfig(convertScopes=true) with auto-producers
Browse files Browse the repository at this point in the history
The annotation transformer in `SingletonToApplicationScopedTestBuildChainCustomizerProducer`
has to:

- look for the `@Produces` annotation in ArC's `AnnotationStore`, not in Jandex;
- run _after_ the annotation transformer in `AutoProducerMethodsProcessor`.

This is enough to recognize an auto-producer (producer without `@Produces`).
  • Loading branch information
Ladicek committed Feb 19, 2024
1 parent 5f0dbbc commit 5aebe5a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package io.quarkus.it.mockbean;

import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;

public class SuffixServiceSingletonProducer {

@Produces
//@Produces // intentionally commented out to test that auto-producers work with `@InjectMock`
@Singleton
public SuffixServiceSingleton dummyService() {
return new SuffixServiceSingleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testInjectedUnusedBeanIsNotRemoved() {
}

@Test
public void testNonInjectedUnusedBeanIsNotRemoved() {
public void testNonInjectedUnusedBeanIsRemoved() {
Assertions.assertFalse(Arc.container().instance(OtherUnusedService.class).isAvailable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void execute(BuildContext context) {
continue;
}
AnnotationValue allowScopeConversionValue = instance.value("convertScopes");
if ((allowScopeConversionValue != null) && allowScopeConversionValue.asBoolean()) {
if (allowScopeConversionValue != null && allowScopeConversionValue.asBoolean()) {
// we need to fetch the type of the bean, so we need to look at the type of the field
mockTypes.add(instance.target().asField().type().name());
}
Expand All @@ -84,7 +84,15 @@ public void execute(BuildContext context) {
context.produce(new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
@Override
public boolean appliesTo(AnnotationTarget.Kind kind) {
return (kind == AnnotationTarget.Kind.CLASS) || (kind == AnnotationTarget.Kind.METHOD);
return kind == AnnotationTarget.Kind.CLASS || kind == AnnotationTarget.Kind.METHOD;
}

@Override
public int getPriority() {
// annotation transformer registered in `AutoProducerMethodsProcessor` has priority
// of `DEFAULT_PRIORITY - 1` and we need to run _after_ it, otherwise we wouldn't
// recognize an auto-producer (producer without `@Produces`)
return DEFAULT_PRIORITY - 10;
}

@Override
Expand All @@ -100,9 +108,8 @@ public void transform(TransformationContext transformationContext) {
}
} else if (target.kind() == AnnotationTarget.Kind.METHOD) { // CDI producer case
MethodInfo methodInfo = target.asMethod();
if ((methodInfo.annotation(DotNames.PRODUCES) != null)
&& (Annotations.contains(transformationContext.getAnnotations(),
DotNames.SINGLETON)
if (Annotations.contains(transformationContext.getAnnotations(), DotNames.PRODUCES)
&& (Annotations.contains(transformationContext.getAnnotations(), DotNames.SINGLETON)
|| hasSingletonBeanDefiningAnnotation(transformationContext))) {
DotName returnType = methodInfo.returnType().name();
if (mockTypes.contains(returnType)) {
Expand Down

0 comments on commit 5aebe5a

Please sign in to comment.