11package org .testcontainers .junit .jupiter ;
22
3+ import lombok .Getter ;
34import org .junit .jupiter .api .extension .BeforeEachCallback ;
45import org .junit .jupiter .api .extension .ExtensionConfigurationException ;
56import org .junit .jupiter .api .extension .ExtensionContext ;
@@ -30,7 +31,7 @@ public void postProcessTestInstance(final Object testInstance, final ExtensionCo
3031 store .put (TEST_INSTANCE , testInstance );
3132
3233 findSharedContainers (testInstance )
33- .forEach (adapter -> store .getOrComputeIfAbsent (adapter .key , k -> adapter .start ()));
34+ .forEach (adapter -> store .getOrComputeIfAbsent (adapter .getKey () , k -> adapter .start ()));
3435 }
3536
3637 @ Override
@@ -39,13 +40,13 @@ public void beforeEach(final ExtensionContext context) {
3940 .parallelStream ()
4041 .flatMap (this ::findRestartContainers )
4142 .forEach (adapter -> context .getStore (NAMESPACE )
42- .getOrComputeIfAbsent (adapter .key , k -> adapter .start ()));
43+ .getOrComputeIfAbsent (adapter .getKey () , k -> adapter .start ()));
4344 }
4445
4546 private Set <Object > collectParentTestInstances (final ExtensionContext context ) {
4647 Set <Object > testInstances = new LinkedHashSet <>();
4748 Optional <ExtensionContext > current = Optional .of (context );
48- while (current .isPresent ()) {
49+ while (current .isPresent ()) {
4950 ExtensionContext ctx = current .get ();
5051 Object testInstance = ctx .getStore (NAMESPACE ).remove (TEST_INSTANCE );
5152 if (testInstance != null ) {
@@ -83,7 +84,18 @@ private Predicate<Field> isRestartContainer() {
8384 }
8485
8586 private static Predicate <Field > isContainer () {
86- return field -> Startable .class .isAssignableFrom (field .getType ()) && AnnotationSupport .isAnnotated (field , Container .class );
87+ return field -> {
88+ boolean isAnnotatedWithContainer = AnnotationSupport .isAnnotated (field , Container .class );
89+ if (isAnnotatedWithContainer ) {
90+ boolean isStartable = Startable .class .isAssignableFrom (field .getType ());
91+
92+ if (!isStartable ) {
93+ throw new ExtensionConfigurationException ("Annotation is only supported for Startable types" );
94+ }
95+ return true ;
96+ }
97+ return false ;
98+ };
8799 }
88100
89101 private static StoreAdapter getContainerInstance (final Object testInstance , final Field field ) {
@@ -103,6 +115,7 @@ private static StoreAdapter getContainerInstance(final Object testInstance, fina
103115 */
104116 private static class StoreAdapter implements CloseableResource {
105117
118+ @ Getter
106119 private String key ;
107120
108121 private Startable container ;
0 commit comments