1
1
/*
2
- * Copyright (c) 2020, 2023 Oracle and/or its affiliates.
2
+ * Copyright (c) 2020, 2024 Oracle and/or its affiliates.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import java .util .ArrayList ;
29
29
import java .util .Arrays ;
30
30
import java .util .HashMap ;
31
+ import java .util .HashSet ;
31
32
import java .util .List ;
32
33
import java .util .Map ;
33
34
import java .util .Set ;
48
49
import jakarta .enterprise .inject .spi .CDI ;
49
50
import jakarta .enterprise .inject .spi .Extension ;
50
51
import jakarta .enterprise .inject .spi .InjectionPoint ;
52
+ import jakarta .enterprise .inject .spi .ProcessAnnotatedType ;
51
53
import jakarta .enterprise .inject .spi .ProcessInjectionPoint ;
54
+ import jakarta .enterprise .inject .spi .WithAnnotations ;
55
+ import jakarta .enterprise .inject .spi .configurator .AnnotatedFieldConfigurator ;
52
56
import jakarta .enterprise .inject .spi .configurator .AnnotatedTypeConfigurator ;
53
57
import jakarta .enterprise .util .AnnotationLiteral ;
54
58
import jakarta .inject .Inject ;
61
65
import org .eclipse .microprofile .config .spi .ConfigProviderResolver ;
62
66
import org .eclipse .microprofile .config .spi .ConfigSource ;
63
67
import org .glassfish .jersey .ext .cdi1x .internal .CdiComponentProvider ;
68
+ import org .mockito .Mockito ;
64
69
import org .junit .jupiter .api .Test ;
65
70
import org .junit .jupiter .api .TestInstance ;
66
71
import org .junit .jupiter .api .extension .AfterAllCallback ;
@@ -288,6 +293,11 @@ private void validatePerTest() {
288
293
+ " injection into fields or constructor is not supported, as each"
289
294
+ " test method uses a different CDI container. Field " + field
290
295
+ " is annotated with @Inject" );
296
+ } else if (field .getAnnotation (MockBean .class ) != null ) {
297
+ throw new RuntimeException ("When a class is annotated with @HelidonTest(resetPerTest=true),"
298
+ + " injection into fields or constructor is not supported, as each"
299
+ + " test method uses a different CDI container. Field " + field
300
+ + " is annotated with @MockBean" );
291
301
}
292
302
}
293
303
@@ -298,6 +308,11 @@ private void validatePerTest() {
298
308
+ " injection into fields or constructor is not supported, as each"
299
309
+ " test method uses a different CDI container. Field " + field
300
310
+ " is annotated with @Inject" );
311
+ } else if (field .getAnnotation (MockBean .class ) != null ) {
312
+ throw new RuntimeException ("When a class is annotated with @HelidonTest(resetPerTest=true),"
313
+ + " injection into fields or constructor is not supported, as each"
314
+ + " test method uses a different CDI container. Field " + field
315
+ + " is annotated with @MockBean" );
301
316
}
302
317
}
303
318
}
@@ -504,6 +519,7 @@ private static class AddBeansExtension implements Extension {
504
519
private final List <AddBean > addBeans ;
505
520
506
521
private final HashMap <String , Annotation > socketAnnotations = new HashMap <>();
522
+ private final Set <Class <?>> mocks = new HashSet <>();
507
523
508
524
private AddBeansExtension (Class <?> testClass , List <AddBean > addBeans ) {
509
525
this .testClass = testClass ;
@@ -521,7 +537,18 @@ void processSocketInjectionPoints(@Observes ProcessInjectionPoint<?, WebTarget>
521
537
break ;
522
538
}
523
539
}
540
+ }
524
541
542
+ void processMockBean (@ Observes @ WithAnnotations (MockBean .class ) ProcessAnnotatedType <?> obj ) throws Exception {
543
+ var configurator = obj .configureAnnotatedType ();
544
+ for (AnnotatedFieldConfigurator <?> field : configurator .fields ()) {
545
+ MockBean mockBean = field .getAnnotated ().getAnnotation (MockBean .class );
546
+ if (mockBean != null ) {
547
+ Field f = field .getAnnotated ().getJavaMember ();
548
+ Class <?> fieldType = f .getType ();
549
+ mocks .add (fieldType );
550
+ }
551
+ }
525
552
}
526
553
527
554
void registerOtherBeans (@ Observes AfterBeanDiscovery event ) {
@@ -543,6 +570,13 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event) {
543
570
.scope (ApplicationScoped .class )
544
571
.createWith (context -> getWebTarget (client , "@default" ));
545
572
573
+ // Register all mocks
574
+ mocks .forEach (type -> {
575
+ event .addBean ()
576
+ .addType (type )
577
+ .scope (ApplicationScoped .class )
578
+ .createWith (inst -> Mockito .mock (type ));
579
+ });
546
580
}
547
581
548
582
@ SuppressWarnings ("unchecked" )
0 commit comments