Skip to content

Commit 39f9fb0

Browse files
committed
Introduce MockTestcontainersConfiguration JUnit Jupiter extension
This is the replacement for MockTestcontainersConfigurationRule for internal JUnit Jupiter tests. This change removes references to BlockJUnit4ClassRunner. Tested with: ./gradlew :testcontainers:test --tests 'org.testcontainers.containers.ReusabilityUnitTests$*'
1 parent 0ef58b5 commit 39f9fb0

File tree

3 files changed

+80
-32
lines changed

3 files changed

+80
-32
lines changed

core/src/test/java/org/testcontainers/containers/ReusabilityUnitTests.java

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,24 @@
99
import com.github.dockerjava.api.command.InspectContainerResponse;
1010
import com.github.dockerjava.api.command.ListContainersCmd;
1111
import com.github.dockerjava.api.command.StartContainerCmd;
12+
import com.github.dockerjava.api.model.Container;
1213
import com.github.dockerjava.core.command.CreateContainerCmdImpl;
1314
import com.github.dockerjava.core.command.InspectContainerCmdImpl;
1415
import com.github.dockerjava.core.command.ListContainersCmdImpl;
1516
import com.github.dockerjava.core.command.StartContainerCmdImpl;
1617
import lombok.RequiredArgsConstructor;
1718
import lombok.experimental.FieldDefaults;
18-
import org.junit.Rule;
19-
import org.junit.jupiter.api.Nested;
2019
import org.junit.jupiter.api.Test;
2120
import org.junit.jupiter.params.ParameterizedClass;
2221
import org.junit.jupiter.params.provider.MethodSource;
23-
import org.junit.runner.RunWith;
24-
import org.junit.runners.BlockJUnit4ClassRunner;
2522
import org.mockito.Answers;
2623
import org.mockito.Mockito;
2724
import org.mockito.stubbing.Answer;
2825
import org.testcontainers.DockerClientFactory;
2926
import org.testcontainers.TestImages;
3027
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
3128
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
32-
import org.testcontainers.utility.MockTestcontainersConfigurationRule;
29+
import org.testcontainers.utility.MockTestcontainersConfiguration;
3330
import org.testcontainers.utility.MountableFile;
3431
import org.testcontainers.utility.TestcontainersConfiguration;
3532

@@ -55,13 +52,12 @@
5552

5653
public class ReusabilityUnitTests {
5754

58-
@Nested
5955
@ParameterizedClass
6056
@MethodSource("data")
6157
@RequiredArgsConstructor
6258
@FieldDefaults(makeFinal = true)
63-
public class CanBeReusedTest {
64-
public Object[][] data() {
59+
public static class CanBeReusedTest {
60+
public static Object[][] data() {
6561
return new Object[][] {
6662
{ "generic", new GenericContainer<>(TestImages.TINY_IMAGE), true },
6763
{ "anonymous generic", new GenericContainer(TestImages.TINY_IMAGE) {}, true },
@@ -86,14 +82,14 @@ public void shouldBeReusable() {
8682
}
8783
}
8884

89-
class CustomContainer extends GenericContainer<CustomContainer> {
85+
static class CustomContainer extends GenericContainer<CustomContainer> {
9086

9187
CustomContainer() {
9288
super(TestImages.TINY_IMAGE);
9389
}
9490
}
9591

96-
class CustomContainerWithContainerIsCreated
92+
static class CustomContainerWithContainerIsCreated
9793
extends GenericContainer<CustomContainerWithContainerIsCreated> {
9894

9995
CustomContainerWithContainerIsCreated() {
@@ -107,10 +103,8 @@ protected void containerIsCreated(String containerId) {
107103
}
108104
}
109105

110-
@Nested
111-
@RunWith(BlockJUnit4ClassRunner.class)
112106
@FieldDefaults(makeFinal = true)
113-
public class HooksTest extends AbstractReusabilityTest {
107+
public static class HooksTest extends AbstractReusabilityTest {
114108

115109
List<String> script = new ArrayList<>();
116110

@@ -189,10 +183,8 @@ public void shouldNotCallHookIfNotReused() {
189183
}
190184
}
191185

192-
@Nested
193-
@RunWith(BlockJUnit4ClassRunner.class)
194186
@FieldDefaults(makeFinal = true)
195-
public class HashTest extends AbstractReusabilityTest {
187+
public static class HashTest extends AbstractReusabilityTest {
196188

197189
protected GenericContainer<?> container = makeReusable(
198190
new GenericContainer(TestImages.TINY_IMAGE) {
@@ -304,22 +296,20 @@ public void shouldHashCopiedFiles() {
304296
}
305297
}
306298

307-
308-
interface TestStrategy {
309-
void withCopyFileToContainer(MountableFile mountableFile, String path);
310-
311-
void clear();
312-
}
313-
314-
@Nested
315299
@ParameterizedClass
316300
@MethodSource("strategies")
317301
@FieldDefaults(makeFinal = true)
318-
public class CopyFilesHashTest {
302+
public static class CopyFilesHashTest {
319303

320304
private final TestStrategy strategy;
321305

322-
private class MountableFileTestStrategy implements TestStrategy {
306+
interface TestStrategy {
307+
void withCopyFileToContainer(MountableFile mountableFile, String path);
308+
309+
void clear();
310+
}
311+
312+
private static class MountableFileTestStrategy implements TestStrategy {
323313

324314
private final GenericContainer<?> container;
325315

@@ -338,7 +328,7 @@ public void clear() {
338328
}
339329
}
340330

341-
private class TransferableTestStrategy implements TestStrategy {
331+
private static class TransferableTestStrategy implements TestStrategy {
342332

343333
private final GenericContainer<?> container;
344334

@@ -357,7 +347,7 @@ public void clear() {
357347
}
358348
}
359349

360-
public List<Function<GenericContainer<?>, TestStrategy>> strategies() {
350+
public static List<Function<GenericContainer<?>, TestStrategy>> strategies() {
361351
return Arrays.asList(MountableFileTestStrategy::new, TransferableTestStrategy::new);
362352
}
363353

@@ -508,11 +498,9 @@ public void folderPermissions() throws Exception {
508498
}
509499
}
510500

501+
@MockTestcontainersConfiguration
511502
@FieldDefaults(makeFinal = true)
512-
public abstract class AbstractReusabilityTest {
513-
514-
@Rule
515-
public MockTestcontainersConfigurationRule configurationMock = new MockTestcontainersConfigurationRule();
503+
public abstract static class AbstractReusabilityTest {
516504

517505
protected DockerClient client = Mockito.mock(DockerClient.class);
518506

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.testcontainers.utility;
2+
3+
import org.junit.jupiter.api.extension.ExtendWith;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Inherited;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* A JUnit Jupiter extension that applies a spy on {@link TestcontainersConfiguration}
13+
* for testing features that depend on the global configuration.
14+
*/
15+
@Target(ElementType.TYPE)
16+
@Retention(RetentionPolicy.RUNTIME)
17+
@ExtendWith(MockTestcontainersConfigurationExtension.class)
18+
@Inherited
19+
public @interface MockTestcontainersConfiguration {
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.testcontainers.utility;
2+
3+
import org.junit.jupiter.api.extension.AfterEachCallback;
4+
import org.junit.jupiter.api.extension.BeforeEachCallback;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
7+
import org.junit.jupiter.api.extension.ExtensionContext.Store;
8+
import org.mockito.Mockito;
9+
10+
import java.util.concurrent.atomic.AtomicReference;
11+
12+
/**
13+
* This extension applies a spy on {@link TestcontainersConfiguration}
14+
* for testing features that depend on the global configuration.
15+
*/
16+
class MockTestcontainersConfigurationExtension implements BeforeEachCallback, AfterEachCallback {
17+
18+
private static final Namespace NAMESPACE = Namespace.create(MockTestcontainersConfigurationExtension.class);
19+
20+
private static final String PREVIOUS_REF = "previousRef";
21+
22+
private static AtomicReference<TestcontainersConfiguration> REF = TestcontainersConfiguration.getInstanceField();
23+
24+
@Override
25+
public void beforeEach(ExtensionContext context) {
26+
TestcontainersConfiguration previous = REF.get();
27+
if (previous == null) {
28+
previous = TestcontainersConfiguration.getInstance();
29+
}
30+
REF.set(Mockito.spy(previous));
31+
context.getStore(NAMESPACE).put(PREVIOUS_REF, previous);
32+
}
33+
34+
@Override
35+
public void afterEach(ExtensionContext context) {
36+
TestcontainersConfiguration previous = context.getStore(NAMESPACE).get(PREVIOUS_REF, TestcontainersConfiguration.class);
37+
REF.set(previous);
38+
Mockito.validateMockitoUsage();
39+
}
40+
}

0 commit comments

Comments
 (0)