Skip to content

Commit f7537a4

Browse files
committed
Migrate from JUnit 4 to 5
This is necessary for Testcontainers 2.x
1 parent 10328a9 commit f7537a4

26 files changed

+320
-173
lines changed

build.gradle.kts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ dependencies {
4747
}
4848
implementation("org.connectbot:jbcrypt:1.0.2")
4949

50-
testImplementation("junit:junit:4.13.2")
51-
testImplementation("commons-io:commons-io:2.20.0")
52-
testImplementation("commons-codec:commons-codec:1.19.0")
53-
testImplementation("org.testcontainers:testcontainers:1.21.3")
54-
testImplementation("org.jetbrains:annotations:26.0.2")
5550
testImplementation("ch.qos.logback:logback-classic:1.5.20")
51+
testImplementation("commons-codec:commons-codec:1.19.0")
52+
testImplementation("commons-io:commons-io:2.20.0")
5653
testImplementation("org.hamcrest:hamcrest:3.0")
54+
testImplementation("org.jetbrains:annotations:26.0.2")
55+
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.4")
56+
testImplementation("org.junit.jupiter:junit-jupiter-params:5.11.4")
5757
testImplementation("org.mockito:mockito-core:5.19.0")
58+
testImplementation("org.mockito:mockito-junit-jupiter:5.19.0")
59+
testImplementation("org.testcontainers:junit-jupiter:1.21.3")
60+
testImplementation("org.testcontainers:testcontainers:1.21.3")
61+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.4")
62+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
5863
}
5964

6065
java {
@@ -70,6 +75,10 @@ tasks.withType<ShadowJar> {
7075
minimize()
7176
}
7277

78+
tasks.test {
79+
useJUnitPlatform()
80+
}
81+
7382
tasks.jacocoTestReport {
7483
reports {
7584
xml.required.set(true)

src/test/java/com/trilead/ssh2/AsyncSSHCompatibilityTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,39 @@
33
import org.apache.commons.io.IOUtils;
44
import org.jetbrains.annotations.Contract;
55
import org.jetbrains.annotations.NotNull;
6-
import org.junit.ClassRule;
7-
import org.junit.Rule;
8-
import org.junit.Test;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.RegisterExtension;
98
import org.slf4j.Logger;
109
import org.slf4j.LoggerFactory;
1110
import org.testcontainers.containers.GenericContainer;
1211
import org.testcontainers.containers.output.Slf4jLogConsumer;
1312
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
1413
import org.testcontainers.images.builder.ImageFromDockerfile;
14+
import org.testcontainers.junit.jupiter.Container;
15+
import org.testcontainers.junit.jupiter.Testcontainers;
1516

1617
import java.io.IOException;
1718

1819
import static org.hamcrest.CoreMatchers.is;
19-
import static org.junit.Assert.assertThat;
20+
import static org.hamcrest.MatcherAssert.assertThat;
2021

2122
/**
22-
* Integration tests against OpenSSH.
23+
* Integration tests against AsyncSSH.
2324
*
2425
* @author Kenny Root
2526
*/
27+
@Testcontainers
2628
public class AsyncSSHCompatibilityTest {
2729
private static final Logger logger = LoggerFactory.getLogger(AsyncSSHCompatibilityTest.class.getSimpleName());
2830

29-
@Rule
31+
@RegisterExtension
3032
public SshLogger sshLogger = new SshLogger(logger);
3133

3234
private static final String USERNAME = "user123";
3335
private static final String PASSWORD = "secretpw";
3436

35-
@ClassRule
36-
public static GenericContainer server;
37+
@Container
38+
public static GenericContainer<?> server;
3739

3840
static {
3941
ImageFromDockerfile baseImage = new ImageFromDockerfile("asyncssh-server", false)
@@ -42,7 +44,7 @@ public class AsyncSSHCompatibilityTest {
4244
baseImage.withFileFromClasspath(key, "com/trilead/ssh2/crypto/" + key);
4345
}
4446

45-
server = new GenericContainer(baseImage)
47+
server = new GenericContainer<>(baseImage)
4648
.withExposedPorts(8022)
4749
.withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("DOCKER"))
4850
.waitingFor(new LogMessageWaitStrategy()

src/test/java/com/trilead/ssh2/DropbearCompatibilityTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@
44
import org.jetbrains.annotations.Contract;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
7-
import org.junit.ClassRule;
8-
import org.junit.Rule;
9-
import org.junit.Test;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.RegisterExtension;
109
import org.slf4j.Logger;
1110
import org.slf4j.LoggerFactory;
1211
import org.testcontainers.containers.GenericContainer;
1312
import org.testcontainers.containers.output.Slf4jLogConsumer;
1413
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
1514
import org.testcontainers.images.builder.ImageFromDockerfile;
15+
import org.testcontainers.junit.jupiter.Container;
16+
import org.testcontainers.junit.jupiter.Testcontainers;
1617

1718
import java.io.IOException;
1819
import java.util.function.Consumer;
1920

2021
import static org.hamcrest.CoreMatchers.is;
21-
import static org.junit.Assert.assertThat;
22+
import static org.hamcrest.MatcherAssert.assertThat;
2223

2324
/**
2425
* Integration tests against Dropbear.
2526
*
2627
* @author Kenny Root
2728
*/
29+
@Testcontainers
2830
public class DropbearCompatibilityTest {
2931
private static final Logger logger = LoggerFactory.getLogger(DropbearCompatibilityTest.class.getSimpleName());
3032
private static final Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger).withPrefix("DOCKER");
3133

32-
@Rule
34+
@RegisterExtension
3335
public SshLogger sshLogger = new SshLogger(logger);
3436

3537
private static final String OPTIONS_ENV = "OPTIONS";
@@ -45,8 +47,8 @@ public class DropbearCompatibilityTest {
4547
}
4648
}
4749

48-
@ClassRule
49-
public static GenericContainer server = getBaseContainer();
50+
@Container
51+
public static GenericContainer<?> server = getBaseContainer();
5052

5153
@NotNull
5254
@Contract("_ -> new")

src/test/java/com/trilead/ssh2/ExtensionInfoTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.trilead.ssh2;
22

33
import com.trilead.ssh2.packets.PacketExtInfo;
4-
import org.junit.Test;
4+
import org.junit.jupiter.api.Test;
55

66
import java.util.Collections;
77
import java.util.Set;
88

9-
import static org.junit.Assert.assertEquals;
10-
import static org.junit.Assert.assertTrue;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
1111

1212
public class ExtensionInfoTest {
1313
@Test

src/test/java/com/trilead/ssh2/KnownHostsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
77
import org.hamcrest.TypeSafeMatcher;
8-
import org.junit.Test;
8+
import org.junit.jupiter.api.Test;
99

1010
import java.io.IOException;
1111
import java.lang.reflect.InvocationTargetException;

src/test/java/com/trilead/ssh2/OpenSSHCompatibilityTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.apache.commons.io.IOUtils;
44
import org.jetbrains.annotations.Contract;
55
import org.jetbrains.annotations.NotNull;
6-
import org.junit.Rule;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.RegisterExtension;
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010
import org.testcontainers.containers.GenericContainer;
@@ -26,7 +26,7 @@ public class OpenSSHCompatibilityTest {
2626
private static final Logger logger = LoggerFactory.getLogger(OpenSSHCompatibilityTest.class.getSimpleName());
2727
private static final Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger).withPrefix("DOCKER");
2828

29-
@Rule
29+
@RegisterExtension
3030
public SshLogger sshLogger = new SshLogger(logger);
3131

3232
private static final String OPTIONS_ENV = "OPTIONS";
Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package com.trilead.ssh2;
22

33
import static org.mockito.Mockito.mock;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.io.IOException;
6-
import org.junit.Before;
7-
import org.junit.Test;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
810
import org.mockito.Mock;
911
import org.mockito.MockitoAnnotations;
12+
import org.mockito.junit.jupiter.MockitoExtension;
1013

14+
@ExtendWith(MockitoExtension.class)
1115
public class SCPClientTest {
1216

1317
@Mock
1418
private Connection mockConnection;
1519

1620
private SCPClient scpClient;
1721

18-
@Before
22+
@BeforeEach
1923
public void setUp() {
2024
MockitoAnnotations.initMocks(this);
2125
scpClient = new SCPClient(mockConnection);
@@ -27,14 +31,18 @@ public void testSCPClientConstruction() {
2731
SCPClient client = new SCPClient(conn);
2832
}
2933

30-
@Test(expected = IllegalArgumentException.class)
31-
public void testSCPClientConstructionWithNullConnection() {
34+
@Test
35+
public void testSCPClientConstructionWithNullConnection() {
36+
assertThrows(IllegalArgumentException.class, () -> {
3237
new SCPClient(null);
38+
});
3339
}
3440

35-
@Test(expected = IllegalArgumentException.class)
36-
public void testPutWithNullLocalFiles() throws IOException {
41+
@Test
42+
public void testPutWithNullLocalFiles() throws IOException {
43+
assertThrows(IllegalArgumentException.class, () -> {
3744
scpClient.put((String[]) null, "/tmp", "0644");
45+
});
3846
}
3947

4048
@Test
@@ -43,38 +51,50 @@ public void testPutWithEmptyLocalFiles() throws IOException {
4351
scpClient.put(new String[0], "/tmp", "0644");
4452
}
4553

46-
@Test(expected = IllegalArgumentException.class)
47-
public void testPutWithNullLocalFileInArray() throws IOException {
54+
@Test
55+
public void testPutWithNullLocalFileInArray() throws IOException {
56+
assertThrows(IllegalArgumentException.class, () -> {
4857
scpClient.put(new String[] { "file1.txt", null, "file2.txt" }, "/tmp",
4958
"0644");
59+
});
5060
}
5161

52-
@Test(expected = IllegalArgumentException.class)
53-
public void testPutDataWithNullRemoteFileName() throws IOException {
62+
@Test
63+
public void testPutDataWithNullRemoteFileName() throws IOException {
64+
assertThrows(IllegalArgumentException.class, () -> {
5465
byte[] data = "Hello World".getBytes();
5566
scpClient.put(data, null, "/tmp", "0644");
67+
});
5668
}
5769

58-
@Test(expected = IllegalArgumentException.class)
59-
public void testPutDataWithNullRemoteTargetDirectory() throws IOException {
70+
@Test
71+
public void testPutDataWithNullRemoteTargetDirectory() throws IOException {
72+
assertThrows(IllegalArgumentException.class, () -> {
6073
byte[] data = "Hello World".getBytes();
6174
scpClient.put(data, "remote.txt", null, "0644");
75+
});
6276
}
6377

64-
@Test(expected = IllegalArgumentException.class)
65-
public void testPutDataWithNullMode() throws IOException {
78+
@Test
79+
public void testPutDataWithNullMode() throws IOException {
80+
assertThrows(IllegalArgumentException.class, () -> {
6681
byte[] data = "Hello World".getBytes();
6782
scpClient.put(data, "remote.txt", "/tmp", null);
83+
});
6884
}
6985

70-
@Test(expected = IllegalArgumentException.class)
71-
public void testGetWithNullRemoteFiles() throws IOException {
86+
@Test
87+
public void testGetWithNullRemoteFiles() throws IOException {
88+
assertThrows(IllegalArgumentException.class, () -> {
7289
scpClient.get((String[]) null, "/tmp");
90+
});
7391
}
7492

75-
@Test(expected = IllegalArgumentException.class)
76-
public void testGetWithNullLocalTargetDirectory() throws IOException {
93+
@Test
94+
public void testGetWithNullLocalTargetDirectory() throws IOException {
95+
assertThrows(IllegalArgumentException.class, () -> {
7796
scpClient.get(new String[] { "file1.txt" }, null);
97+
});
7898
}
7999

80100
@Test
@@ -83,29 +103,39 @@ public void testGetWithEmptyRemoteFiles() throws IOException {
83103
scpClient.get(new String[0], "/tmp");
84104
}
85105

86-
@Test(expected = IllegalArgumentException.class)
87-
public void testGetWithNullRemoteFileInArray() throws IOException {
106+
@Test
107+
public void testGetWithNullRemoteFileInArray() throws IOException {
108+
assertThrows(IllegalArgumentException.class, () -> {
88109
scpClient.get(new String[] { "file1.txt", null }, "/tmp");
110+
});
89111
}
90112

91-
@Test(expected = IllegalArgumentException.class)
92-
public void testGetWithEmptyRemoteFileInArray() throws IOException {
113+
@Test
114+
public void testGetWithEmptyRemoteFileInArray() throws IOException {
115+
assertThrows(IllegalArgumentException.class, () -> {
93116
scpClient.get(new String[] { "file1.txt", "" }, "/tmp");
117+
});
94118
}
95119

96-
@Test(expected = IllegalArgumentException.class)
97-
public void testPutWithInvalidMode() throws IOException {
120+
@Test
121+
public void testPutWithInvalidMode() throws IOException {
122+
assertThrows(IllegalArgumentException.class, () -> {
98123
scpClient.put(new String[] { "file1.txt" }, "/tmp", "invalid");
124+
});
99125
}
100126

101-
@Test(expected = IllegalArgumentException.class)
102-
public void testPutWithShortMode() throws IOException {
127+
@Test
128+
public void testPutWithShortMode() throws IOException {
129+
assertThrows(IllegalArgumentException.class, () -> {
103130
scpClient.put(new String[] { "file1.txt" }, "/tmp", "644");
131+
});
104132
}
105133

106-
@Test(expected = IllegalArgumentException.class)
107-
public void testPutDataWithInvalidMode() throws IOException {
134+
@Test
135+
public void testPutDataWithInvalidMode() throws IOException {
136+
assertThrows(IllegalArgumentException.class, () -> {
108137
byte[] data = "Hello World".getBytes();
109138
scpClient.put(data, "remote.txt", "/tmp", "invalid");
139+
});
110140
}
111141
}

src/test/java/com/trilead/ssh2/SFTPv3ClientTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.IOException;
1818

19+
import org.junit.jupiter.api.extension.ExtendWith;
1920
import org.junit.Before;
2021
import org.junit.Test;
2122
import org.mockito.Mock;
23+
import org.mockito.junit.jupiter.MockitoExtension;
2224
import org.mockito.MockitoAnnotations;
2325

26+
@ExtendWith(MockitoExtension.class)
2427
public class SFTPv3ClientTest {
2528

2629
@Mock

0 commit comments

Comments
 (0)