Skip to content

Migrate tests to JUnit 5 #10349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ subprojects {
}
}

tasks.withType(Test).all {
tasks.withType(Test).configureEach {
useJUnitPlatform()
reports {
junitXml.outputPerTestCase = true
}
Expand All @@ -132,6 +133,10 @@ subprojects {

dependencies {
testImplementation 'ch.qos.logback:logback-classic:1.3.14'
testImplementation(platform("org.junit:junit-bom:5.13.0"))
testImplementation(project(":junit-jupiter"))
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

checkstyle {
Expand Down
10 changes: 8 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ configurations.all {
}

dependencies {
api 'junit:junit:4.13.2'
constraints {
testCompileOnly('junit:junit') {
version {
rejectAll()
}
}
}
api 'org.slf4j:slf4j-api:1.7.36'
compileOnly 'org.jetbrains:annotations:24.1.0'
testCompileOnly 'org.jetbrains:annotations:24.1.0'
Expand Down Expand Up @@ -125,7 +131,7 @@ dependencies {

jarFileTestCompileOnly "org.projectlombok:lombok:${lombok.version}"
jarFileTestAnnotationProcessor "org.projectlombok:lombok:${lombok.version}"
jarFileTestImplementation 'junit:junit:4.13.2'
jarFileTestImplementation(project(":junit-jupiter"))
jarFileTestImplementation 'org.assertj:assertj-core:3.26.3'
jarFileTestImplementation 'org.ow2.asm:asm-debug-all:5.2'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import lombok.RequiredArgsConstructor;
import org.assertj.core.api.Assertions;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.MethodSource;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
Expand All @@ -27,12 +25,13 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;

/**
* This test checks that we don't expose any shaded class in our public API.
* We use {@link Parameterized} runner here to create a test per public class in Testcontainers' JAR file.
*/
@RunWith(Parameterized.class)
@ParameterizedClass
@MethodSource("data")
@RequiredArgsConstructor
public class PublicBinaryAPITest extends AbstractJarFileTest {

Expand All @@ -46,7 +45,6 @@ public class PublicBinaryAPITest extends AbstractJarFileTest {
Assertions.registerFormatterForType(MethodNode.class, it -> it.name + it.desc);
}

@Parameters(name = "{0}")
public static List<Object[]> data() throws Exception {
List<Object[]> result = new ArrayList<>();

Expand Down Expand Up @@ -89,15 +87,15 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO

private final ClassNode classNode;

@Before
@BeforeEach
public void setUp() {
switch (classNode.name) {
// Necessary evil
case "org/testcontainers/dockerclient/UnixSocketClientProviderStrategy":
case "org/testcontainers/dockerclient/DockerClientProviderStrategy":
case "org/testcontainers/dockerclient/WindowsClientProviderStrategy":
case "org/testcontainers/utility/DynamicPollInterval":
Assume.assumeTrue(false);
assumeThat(true).isFalse();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
Expand All @@ -31,7 +29,7 @@
* It uses either Compose V2 contained within the Docker binary, or a containerised version of Compose V2.
*/
@Slf4j
public class ComposeContainer extends FailureDetectingExternalResource implements Startable {
public class ComposeContainer implements Startable {

private final Map<String, Integer> scalingPreferences = new HashMap<>();

Expand Down Expand Up @@ -93,32 +91,6 @@ public ComposeContainer(String identifier, List<File> composeFiles) {
this.project = this.composeDelegate.getProject();
}

@Override
@Deprecated
public Statement apply(Statement base, Description description) {
return super.apply(base, description);
}

@Override
@Deprecated
public void starting(Description description) {
start();
}

@Override
@Deprecated
protected void succeeded(Description description) {}

@Override
@Deprecated
protected void failed(Throwable e, Description description) {}

@Override
@Deprecated
public void finished(Description description) {
stop();
}

@Override
public void start() {
synchronized (MUTEX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
Expand All @@ -31,7 +29,7 @@
*/
@Slf4j
public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>>
extends FailureDetectingExternalResource

implements Startable {

private final Map<String, Integer> scalingPreferences = new HashMap<>();
Expand Down Expand Up @@ -99,32 +97,6 @@ public DockerComposeContainer(String identifier, List<File> composeFiles) {
this.project = this.composeDelegate.getProject();
}

@Override
@Deprecated
public Statement apply(Statement base, Description description) {
return super.apply(base, description);
}

@Override
@Deprecated
public void starting(Description description) {
start();
}

@Override
@Deprecated
protected void succeeded(Description description) {}

@Override
@Deprecated
protected void failed(Throwable e, Description description) {}

@Override
@Deprecated
public void finished(Description description) {
stop();
}

@Override
public void start() {
synchronized (MUTEX) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.slf4j.Logger;
import org.testcontainers.DockerClientFactory;
Expand Down Expand Up @@ -106,7 +104,7 @@
*/
@Data
public class GenericContainer<SELF extends GenericContainer<SELF>>
extends FailureDetectingExternalResource

implements Container<SELF>, AutoCloseable, WaitStrategyTarget, Startable {

public static final int CONTAINER_RUNNING_TIMEOUT_SEC = 30;
Expand Down Expand Up @@ -1053,57 +1051,6 @@ public void addExposedPorts(int... ports) {
this.containerDef.addExposedTcpPorts(ports);
}

private TestDescription toDescription(Description description) {
return new TestDescription() {
@Override
public String getTestId() {
return description.getDisplayName();
}

@Override
public String getFilesystemFriendlyName() {
return description.getClassName() + "-" + description.getMethodName();
}
};
}

@Override
@Deprecated
public Statement apply(Statement base, Description description) {
return super.apply(base, description);
}

@Override
@Deprecated
protected void starting(Description description) {
if (this instanceof TestLifecycleAware) {
((TestLifecycleAware) this).beforeTest(toDescription(description));
}
this.start();
}

@Override
@Deprecated
protected void succeeded(Description description) {
if (this instanceof TestLifecycleAware) {
((TestLifecycleAware) this).afterTest(toDescription(description), Optional.empty());
}
}

@Override
@Deprecated
protected void failed(Throwable e, Description description) {
if (this instanceof TestLifecycleAware) {
((TestLifecycleAware) this).afterTest(toDescription(description), Optional.of(e));
}
}

@Override
@Deprecated
protected void finished(Description description) {
this.stop();
}

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 2 additions & 9 deletions core/src/main/java/org/testcontainers/containers/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Singular;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.utility.ResourceReaper;

Expand All @@ -17,7 +15,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public interface Network extends AutoCloseable, TestRule {
public interface Network extends AutoCloseable {
Network SHARED = new NetworkImpl(false, null, Collections.emptySet(), null) {
@Override
public void close() {
Expand All @@ -40,7 +38,7 @@ static NetworkImpl.NetworkImplBuilder builder() {

@Builder
@Getter
class NetworkImpl extends ExternalResource implements Network {
class NetworkImpl implements Network {

private final String name = UUID.randomUUID().toString();

Expand Down Expand Up @@ -100,11 +98,6 @@ private String create() {
return createNetworkCmd.exec().getId();
}

@Override
protected void after() {
close();
}

@Override
public synchronized void close() {
if (initialized.getAndSet(false)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package alt.testcontainers.images;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.testcontainers.TestImages;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/testcontainers/DaemonTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.testcontainers;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;

import java.io.File;
Expand Down
Loading