Skip to content

Commit 78a0f69

Browse files
kiviewrnorth
authored andcommitted
Small improvements to Jupiter extension code style (#947)
* Small improvements to Jupiter extension code style Follow up to #887 * Only raise exception if annotated field is not Startable
1 parent 4abfc4f commit 78a0f69

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

modules/junit-jupiter/src/main/java/org/testcontainers/junit/jupiter/TestcontainersExtension.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.testcontainers.junit.jupiter;
22

3+
import lombok.Getter;
34
import org.junit.jupiter.api.extension.BeforeEachCallback;
45
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
56
import 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;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.testcontainers.junit.jupiter;
2+
3+
4+
import org.junit.jupiter.api.Disabled;
5+
import org.junit.jupiter.api.Test;
6+
7+
8+
@Disabled
9+
@Testcontainers
10+
class WrongAnnotationUsageTests {
11+
12+
@Container
13+
private String notStartable = "foobar";
14+
15+
@Test
16+
void extension_throws_exception() {
17+
assert true;
18+
}
19+
20+
}

0 commit comments

Comments
 (0)