Skip to content

Commit d838eb0

Browse files
committed
Introduce MockTestcontainersConfigurationExtension
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 d838eb0

File tree

2 files changed

+67
-32
lines changed

2 files changed

+67
-32
lines changed

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

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,29 @@
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;
20+
import org.junit.jupiter.api.extension.ExtendWith;
2121
import org.junit.jupiter.params.ParameterizedClass;
2222
import org.junit.jupiter.params.provider.MethodSource;
23-
import org.junit.runner.RunWith;
24-
import org.junit.runners.BlockJUnit4ClassRunner;
2523
import org.mockito.Answers;
2624
import org.mockito.Mockito;
2725
import org.mockito.stubbing.Answer;
2826
import org.testcontainers.DockerClientFactory;
2927
import org.testcontainers.TestImages;
28+
import org.testcontainers.containers.ReusabilityUnitTests.CanBeReusedTest.CustomContainer;
29+
import org.testcontainers.containers.ReusabilityUnitTests.CanBeReusedTest.CustomContainerWithContainerIsCreated;
30+
import org.testcontainers.containers.ReusabilityUnitTests.CopyFilesHashTest.TestStrategy;
3031
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
32+
import org.testcontainers.containers.startupcheck.StartupCheckStrategy.StartupStatus;
3133
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
32-
import org.testcontainers.utility.MockTestcontainersConfigurationRule;
34+
import org.testcontainers.utility.MockTestcontainersConfigurationExtension;
3335
import org.testcontainers.utility.MountableFile;
3436
import org.testcontainers.utility.TestcontainersConfiguration;
3537

@@ -55,13 +57,13 @@
5557

5658
public class ReusabilityUnitTests {
5759

58-
@Nested
5960
@ParameterizedClass
6061
@MethodSource("data")
6162
@RequiredArgsConstructor
6263
@FieldDefaults(makeFinal = true)
63-
public class CanBeReusedTest {
64-
public Object[][] data() {
64+
public static class CanBeReusedTest {
65+
66+
public static Object[][] data() {
6567
return new Object[][] {
6668
{ "generic", new GenericContainer<>(TestImages.TINY_IMAGE), true },
6769
{ "anonymous generic", new GenericContainer(TestImages.TINY_IMAGE) {}, true },
@@ -86,14 +88,14 @@ public void shouldBeReusable() {
8688
}
8789
}
8890

89-
class CustomContainer extends GenericContainer<CustomContainer> {
91+
static class CustomContainer extends GenericContainer<CustomContainer> {
9092

9193
CustomContainer() {
9294
super(TestImages.TINY_IMAGE);
9395
}
9496
}
9597

96-
class CustomContainerWithContainerIsCreated
98+
static class CustomContainerWithContainerIsCreated
9799
extends GenericContainer<CustomContainerWithContainerIsCreated> {
98100

99101
CustomContainerWithContainerIsCreated() {
@@ -107,10 +109,8 @@ protected void containerIsCreated(String containerId) {
107109
}
108110
}
109111

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

115115
List<String> script = new ArrayList<>();
116116

@@ -189,10 +189,8 @@ public void shouldNotCallHookIfNotReused() {
189189
}
190190
}
191191

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

197195
protected GenericContainer<?> container = makeReusable(
198196
new GenericContainer(TestImages.TINY_IMAGE) {
@@ -304,22 +302,20 @@ public void shouldHashCopiedFiles() {
304302
}
305303
}
306304

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

320310
private final TestStrategy strategy;
321311

322-
private class MountableFileTestStrategy implements TestStrategy {
312+
interface TestStrategy {
313+
void withCopyFileToContainer(MountableFile mountableFile, String path);
314+
315+
void clear();
316+
}
317+
318+
private static class MountableFileTestStrategy implements TestStrategy {
323319

324320
private final GenericContainer<?> container;
325321

@@ -338,7 +334,7 @@ public void clear() {
338334
}
339335
}
340336

341-
private class TransferableTestStrategy implements TestStrategy {
337+
private static class TransferableTestStrategy implements TestStrategy {
342338

343339
private final GenericContainer<?> container;
344340

@@ -357,7 +353,7 @@ public void clear() {
357353
}
358354
}
359355

360-
public List<Function<GenericContainer<?>, TestStrategy>> strategies() {
356+
public static List<Function<GenericContainer<?>, TestStrategy>> strategies() {
361357
return Arrays.asList(MountableFileTestStrategy::new, TransferableTestStrategy::new);
362358
}
363359

@@ -508,11 +504,9 @@ public void folderPermissions() throws Exception {
508504
}
509505
}
510506

507+
@ExtendWith(MockTestcontainersConfigurationExtension.class)
511508
@FieldDefaults(makeFinal = true)
512-
public abstract class AbstractReusabilityTest {
513-
514-
@Rule
515-
public MockTestcontainersConfigurationRule configurationMock = new MockTestcontainersConfigurationRule();
509+
public abstract static class AbstractReusabilityTest {
516510

517511
protected DockerClient client = Mockito.mock(DockerClient.class);
518512

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

0 commit comments

Comments
 (0)