31
31
import java .util .ArrayList ;
32
32
import java .util .Arrays ;
33
33
import java .util .HashMap ;
34
- import java .util .HashSet ;
35
34
import java .util .List ;
36
35
import java .util .Map ;
37
36
import java .util .Properties ;
49
48
import jakarta .enterprise .inject .se .SeContainer ;
50
49
import jakarta .enterprise .inject .se .SeContainerInitializer ;
51
50
import jakarta .enterprise .inject .spi .AfterBeanDiscovery ;
52
- import jakarta .enterprise .inject .spi .AnnotatedParameter ;
53
- import jakarta .enterprise .inject .spi .Bean ;
54
- import jakarta .enterprise .inject .spi .BeanManager ;
55
51
import jakarta .enterprise .inject .spi .BeforeBeanDiscovery ;
56
52
import jakarta .enterprise .inject .spi .CDI ;
57
53
import jakarta .enterprise .inject .spi .Extension ;
58
54
import jakarta .enterprise .inject .spi .InjectionPoint ;
59
- import jakarta .enterprise .inject .spi .ProcessAnnotatedType ;
60
55
import jakarta .enterprise .inject .spi .ProcessInjectionPoint ;
61
- import jakarta .enterprise .inject .spi .WithAnnotations ;
62
56
import jakarta .enterprise .inject .spi .configurator .AnnotatedTypeConfigurator ;
63
57
import jakarta .enterprise .util .AnnotationLiteral ;
64
58
import jakarta .inject .Inject ;
83
77
import org .junit .jupiter .api .extension .ParameterResolutionException ;
84
78
import org .junit .jupiter .api .extension .ParameterResolver ;
85
79
import org .junit .jupiter .api .extension .ReflectiveInvocationContext ;
86
- import org .mockito .MockSettings ;
87
- import org .mockito .Mockito ;
88
80
89
81
90
82
/**
@@ -302,11 +294,6 @@ private void validatePerTest() {
302
294
+ " injection into fields or constructor is not supported, as each"
303
295
+ " test method uses a different CDI container. Field " + field
304
296
+ " is annotated with @Inject" );
305
- } else if (field .getAnnotation (MockBean .class ) != null ) {
306
- throw new RuntimeException ("When a class is annotated with @HelidonTest(resetPerTest=true),"
307
- + " injection into fields or constructor is not supported, as each"
308
- + " test method uses a different CDI container. Field " + field
309
- + " is annotated with @MockBean" );
310
297
}
311
298
}
312
299
@@ -317,11 +304,6 @@ private void validatePerTest() {
317
304
+ " injection into fields or constructor is not supported, as each"
318
305
+ " test method uses a different CDI container. Field " + field
319
306
+ " is annotated with @Inject" );
320
- } else if (field .getAnnotation (MockBean .class ) != null ) {
321
- throw new RuntimeException ("When a class is annotated with @HelidonTest(resetPerTest=true),"
322
- + " injection into fields or constructor is not supported, as each"
323
- + " test method uses a different CDI container. Field " + field
324
- + " is annotated with @MockBean" );
325
307
}
326
308
}
327
309
}
@@ -548,13 +530,13 @@ private static class AddBeansExtension implements Extension {
548
530
private final List <AddBean > addBeans ;
549
531
550
532
private final HashMap <String , Annotation > socketAnnotations = new HashMap <>();
551
- private final Set <Class <?>> mocks = new HashSet <>();
552
533
553
534
private AddBeansExtension (Class <?> testClass , List <AddBean > addBeans ) {
554
535
this .testClass = testClass ;
555
536
this .addBeans = addBeans ;
556
537
}
557
538
539
+
558
540
void processSocketInjectionPoints (@ Observes ProcessInjectionPoint <?, WebTarget > event ) throws Exception {
559
541
InjectionPoint injectionPoint = event .getInjectionPoint ();
560
542
Set <Annotation > qualifiers = injectionPoint .getQualifiers ();
@@ -565,36 +547,10 @@ void processSocketInjectionPoints(@Observes ProcessInjectionPoint<?, WebTarget>
565
547
break ;
566
548
}
567
549
}
568
- }
569
550
570
- void processMockBean (@ Observes @ WithAnnotations (MockBean .class ) ProcessAnnotatedType <?> obj ) throws Exception {
571
- var configurator = obj .configureAnnotatedType ();
572
- configurator .fields ().forEach (field -> {
573
- MockBean mockBean = field .getAnnotated ().getAnnotation (MockBean .class );
574
- if (mockBean != null ) {
575
- Field f = field .getAnnotated ().getJavaMember ();
576
- // Adds @Inject to be more user friendly
577
- field .add (Literal .INSTANCE );
578
- Class <?> fieldType = f .getType ();
579
- mocks .add (fieldType );
580
- }
581
- });
582
- configurator .constructors ().forEach (constructor -> {
583
- processMockBeanParameters (constructor .getAnnotated ().getParameters ());
584
- });
585
- }
586
-
587
- private void processMockBeanParameters (List <? extends AnnotatedParameter <?>> parameters ) {
588
- parameters .stream ().forEach (parameter -> {
589
- MockBean mockBean = parameter .getAnnotation (MockBean .class );
590
- if (mockBean != null ) {
591
- Class <?> parameterType = parameter .getJavaParameter ().getType ();
592
- mocks .add (parameterType );
593
- }
594
- });
595
551
}
596
552
597
- void registerOtherBeans (@ Observes AfterBeanDiscovery event , BeanManager beanManager ) {
553
+ void registerOtherBeans (@ Observes AfterBeanDiscovery event ) {
598
554
599
555
Client client = ClientBuilder .newClient ();
600
556
@@ -613,25 +569,6 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event, BeanManager beanMana
613
569
.scope (ApplicationScoped .class )
614
570
.createWith (context -> getWebTarget (client , "@default" ));
615
571
616
- // Register all mocks
617
- mocks .forEach (type -> {
618
- event .addBean ()
619
- .addType (type )
620
- .scope (ApplicationScoped .class )
621
- .alternative (true )
622
- .createWith (inst -> {
623
- Set <Bean <?>> beans = beanManager .getBeans (MockSettings .class );
624
- if (!beans .isEmpty ()) {
625
- Bean <?> bean = beans .iterator ().next ();
626
- MockSettings mockSettings = (MockSettings ) beanManager .getReference (bean , MockSettings .class ,
627
- beanManager .createCreationalContext (null ));
628
- return Mockito .mock (type , mockSettings );
629
- } else {
630
- return Mockito .mock (type );
631
- }
632
- })
633
- .priority (0 );
634
- });
635
572
}
636
573
637
574
@ SuppressWarnings ("unchecked" )
@@ -830,15 +767,4 @@ public Class<? extends Extension> value() {
830
767
}
831
768
}
832
769
833
- /**
834
- * Supports inline instantiation of the {@link Inject} annotation.
835
- */
836
- private static final class Literal extends AnnotationLiteral <Inject > implements Inject {
837
-
838
- private static final Literal INSTANCE = new Literal ();
839
-
840
- @ Serial
841
- private static final long serialVersionUID = 1L ;
842
-
843
- }
844
770
}
0 commit comments