Skip to content

Commit 725095f

Browse files
hasterclaude
andcommitted
Extend named-container test coverage
Adds two test additions on top of the named-lookup implementation: - NamedContainerTest: a third field reusing an existing name, asserting that two fields with the same @TestContainer(name) resolve to the same instance. - TestcontainerRegistryTest: plain-JUnit unit tests exercising the registry's lookup semantics directly — same-name same-instance via lookupOrCreate, lookup of an unknown name, and lookup of the empty string against a registry holding only named containers. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent a515cc6 commit 725095f

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

src/test/java/org/arquillian/testcontainers/NamedContainerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public static JavaArchive createDeployment() {
3333
@Testcontainer(name = "first")
3434
private SimpleTestContainer first;
3535

36+
@Testcontainer(name = "first")
37+
private SimpleTestContainer firstAgain;
38+
3639
@Testcontainer(name = "second")
3740
private SimpleTestContainer second;
3841

@@ -59,4 +62,9 @@ public void checkAllDifferentInstances() {
5962
Assertions.assertNotSame(first, unnamed, "Expected named and unnamed containers to be different instances.");
6063
Assertions.assertNotSame(second, unnamed, "Expected named and unnamed containers to be different instances.");
6164
}
65+
66+
@Test
67+
public void sameNameYieldsSameInstance() {
68+
Assertions.assertSame(first, firstAgain, "Fields sharing a name must resolve to the same instance.");
69+
}
6270
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright The Arquillian Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.arquillian.testcontainers;
7+
8+
import java.lang.reflect.Field;
9+
import java.util.List;
10+
11+
import org.arquillian.testcontainers.api.Testcontainer;
12+
import org.arquillian.testcontainers.common.SimpleTestContainer;
13+
import org.junit.jupiter.api.Assertions;
14+
import org.junit.jupiter.api.Test;
15+
import org.testcontainers.containers.GenericContainer;
16+
17+
/**
18+
* Plain-JUnit unit tests for {@link TestcontainerRegistry} that exercise lookup semantics without
19+
* starting any Docker containers. Annotation instances are obtained by reading the annotations off
20+
* fixture fields on this class so the registry can be driven through {@code lookupOrCreate}
21+
* outside an Arquillian context.
22+
*/
23+
public class TestcontainerRegistryTest {
24+
25+
@Testcontainer(name = "alpha")
26+
private SimpleTestContainer alphaFixture;
27+
28+
@Test
29+
public void sameNameViaLookupOrCreateReturnsSameInstance() throws Exception {
30+
TestcontainerRegistry registry = new TestcontainerRegistry();
31+
GenericContainer<?> first = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
32+
GenericContainer<?> second = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
33+
34+
Assertions.assertSame(first, second);
35+
}
36+
37+
@Test
38+
public void lookupReturnsNullForUnknownName() throws Exception {
39+
TestcontainerRegistry registry = new TestcontainerRegistry();
40+
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
41+
42+
Assertions.assertNull(registry.lookup("missing"));
43+
}
44+
45+
@Test
46+
public void emptyNameLookupDoesNotMatchNamedContainers() throws Exception {
47+
TestcontainerRegistry registry = new TestcontainerRegistry();
48+
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
49+
50+
Assertions.assertNull(registry.lookup(""),
51+
"Empty-string lookup should not return any registered container");
52+
}
53+
54+
@SuppressWarnings("unchecked")
55+
private static Class<GenericContainer<?>> simpleType() {
56+
return (Class<GenericContainer<?>>) (Class<?>) SimpleTestContainer.class;
57+
}
58+
59+
private Testcontainer annotation(final String fieldName) throws NoSuchFieldException {
60+
final Field field = TestcontainerRegistryTest.class.getDeclaredField(fieldName);
61+
return field.getAnnotation(Testcontainer.class);
62+
}
63+
}

0 commit comments

Comments
 (0)