Skip to content

Commit 9d5683f

Browse files
authored
Convert tests to JUnit 5 (#166)
1 parent 336a090 commit 9d5683f

11 files changed

+316
-350
lines changed

plexus-java/pom.xml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,20 @@
2828
<version>1</version>
2929
<optional>true</optional>
3030
</dependency>
31-
32-
<dependency>
33-
<groupId>org.hamcrest</groupId>
34-
<artifactId>hamcrest</artifactId>
35-
<version>2.2</version>
36-
<scope>test</scope>
37-
</dependency>
3831
<dependency>
39-
<groupId>org.hamcrest</groupId>
40-
<artifactId>hamcrest-library</artifactId>
41-
<version>2.2</version>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter</artifactId>
4234
<scope>test</scope>
4335
</dependency>
4436
<dependency>
45-
<groupId>junit</groupId>
46-
<artifactId>junit</artifactId>
47-
<version>4.13.2</version>
37+
<groupId>org.mockito</groupId>
38+
<artifactId>mockito-core</artifactId>
39+
<version>4.11.0</version>
4840
<scope>test</scope>
4941
</dependency>
5042
<dependency>
5143
<groupId>org.mockito</groupId>
52-
<artifactId>mockito-core</artifactId>
44+
<artifactId>mockito-junit-jupiter</artifactId>
5345
<version>4.11.0</version>
5446
<scope>test</scope>
5547
</dependency>
@@ -59,6 +51,12 @@
5951
<version>6.0.0</version>
6052
<scope>test</scope>
6153
</dependency>
54+
<dependency>
55+
<groupId>org.assertj</groupId>
56+
<artifactId>assertj-core</artifactId>
57+
<version>3.24.2</version>
58+
<scope>test</scope>
59+
</dependency>
6260
</dependencies>
6361

6462
<build>

plexus-java/src/test/java/org/codehaus/plexus/languages/java/jpms/AbstractFilenameModuleNameExtractorTest.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,35 @@
2121

2222
import java.nio.file.Paths;
2323

24-
import org.junit.BeforeClass;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2625

27-
import static org.hamcrest.CoreMatchers.not;
28-
import static org.hamcrest.CoreMatchers.startsWith;
29-
import static org.junit.Assert.assertEquals;
30-
import static org.junit.Assume.assumeThat;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertNull;
3128

3229
public abstract class AbstractFilenameModuleNameExtractorTest {
3330
protected abstract ModuleNameExtractor getExtractor();
3431

35-
@BeforeClass
36-
public static void assume() {
37-
assumeThat("Requires at least Java 9", System.getProperty("java.version"), not(startsWith("1.")));
38-
}
39-
4032
@Test
41-
public void testJarWithoutManifest() throws Exception {
33+
void testJarWithoutManifest() throws Exception {
4234
String name = getExtractor().extract(Paths.get("src/test/resources/jar.empty/plexus-java-1.0.0-SNAPSHOT.jar"));
4335
assertEquals("plexus.java", name);
4436
}
4537

4638
@Test
47-
public void testJarWithManifest() throws Exception {
39+
void testJarWithManifest() throws Exception {
4840
String name = getExtractor()
4941
.extract(Paths.get("src/test/resources/jar.manifest.with/plexus-java-1.0.0-SNAPSHOT.jar"));
5042
assertEquals("org.codehaus.plexus.languages.java", name);
5143
}
5244

5345
@Test
54-
public void testJarUnsupported() throws Exception {
46+
void testJarUnsupported() throws Exception {
5547
String name = getExtractor().extract(Paths.get("src/test/resources/jar.unsupported/jdom-1.0.jar"));
56-
assertEquals(null, name);
48+
assertNull(name);
5749
}
5850

5951
@Test
60-
public void testJarWithSpacesInPath() throws Exception {
52+
void testJarWithSpacesInPath() throws Exception {
6153
String name = getExtractor()
6254
.extract(Paths.get("src/test/resources/jar with spaces in path/plexus-java-1.0.0-SNAPSHOT.jar"));
6355
assertEquals("org.codehaus.plexus.languages.java", name);

plexus-java/src/test/java/org/codehaus/plexus/languages/java/jpms/BinaryModuleInfoParserTest.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,29 @@
3333
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaProvides;
3434
import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires;
3535
import org.codehaus.plexus.languages.java.version.JavaVersion;
36-
import org.junit.Test;
36+
import org.junit.jupiter.api.Test;
3737

38-
import static org.hamcrest.MatcherAssert.assertThat;
39-
import static org.hamcrest.Matchers.is;
40-
import static org.junit.Assert.assertArrayEquals;
41-
import static org.junit.Assert.assertEquals;
42-
import static org.junit.Assert.assertNotNull;
43-
import static org.junit.Assert.assertNull;
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
40+
import static org.junit.jupiter.api.Assertions.assertEquals;
41+
import static org.junit.jupiter.api.Assertions.assertFalse;
42+
import static org.junit.jupiter.api.Assertions.assertNotNull;
43+
import static org.junit.jupiter.api.Assertions.assertNull;
44+
import static org.junit.jupiter.api.Assertions.assertThrows;
4445

45-
public class BinaryModuleInfoParserTest {
46-
private BinaryModuleInfoParser parser = new BinaryModuleInfoParser();
46+
class BinaryModuleInfoParserTest {
47+
private final BinaryModuleInfoParser parser = new BinaryModuleInfoParser();
4748

4849
@Test
49-
public void testJarDescriptor() throws Exception {
50+
void testJarDescriptor() throws Exception {
5051
JavaModuleDescriptor descriptor =
5152
parser.getModuleDescriptor(Paths.get("src/test/resources/jar.descriptor/asm-6.0_BETA.jar"));
5253

5354
assertNotNull(descriptor);
54-
assertEquals("org.objectweb.asm", descriptor.name());
55-
assertEquals(false, descriptor.isAutomatic());
55+
assertThat(descriptor.name()).isEqualTo("org.objectweb.asm");
56+
assertFalse(descriptor.isAutomatic());
5657

57-
assertEquals(1, descriptor.requires().size());
58+
assertThat(descriptor.requires()).hasSize(1);
5859
assertEquals("java.base", descriptor.requires().iterator().next().name());
5960

6061
Set<JavaExports> expectedExports = JavaModuleDescriptor.newAutomaticModule("_")
@@ -66,17 +67,17 @@ public void testJarDescriptor() throws Exception {
6667
}
6768

6869
@Test
69-
public void testMultiReleaseJarDescriptor() throws Exception {
70+
void testMultiReleaseJarDescriptor() throws Exception {
7071
JavaModuleDescriptor descriptor = parser.getModuleDescriptor(
7172
Paths.get("src/test/resources/jar.mr.descriptor/jloadr-1.0-SNAPSHOT.jar"), JavaVersion.parse("17"));
7273

7374
assertNotNull(descriptor);
7475
assertEquals("de.adito.jloadr", descriptor.name());
75-
assertEquals(false, descriptor.isAutomatic());
76+
assertFalse(descriptor.isAutomatic());
7677
}
7778

7879
@Test
79-
public void testIncompleteMultiReleaseJarDescriptor() throws Exception {
80+
void testIncompleteMultiReleaseJarDescriptor() throws Exception {
8081
// this jar is missing the Multi-Release: true entry in the Manifest
8182
JavaModuleDescriptor descriptor = parser.getModuleDescriptor(
8283
Paths.get("src/test/resources/jar.mr.incomplete.descriptor/jloadr-1.0-SNAPSHOT.jar"));
@@ -85,23 +86,23 @@ public void testIncompleteMultiReleaseJarDescriptor() throws Exception {
8586
}
8687

8788
@Test
88-
public void testClassicJar() throws Exception {
89+
void testClassicJar() throws Exception {
8990
JavaModuleDescriptor descriptor =
9091
parser.getModuleDescriptor(Paths.get("src/test/resources/jar.empty/plexus-java-1.0.0-SNAPSHOT.jar"));
9192

9293
assertNull(descriptor);
9394
}
9495

9596
@Test
96-
public void testOutputDirectoryDescriptor() throws Exception {
97+
void testOutputDirectoryDescriptor() throws Exception {
9798
JavaModuleDescriptor descriptor =
9899
parser.getModuleDescriptor(Paths.get("src/test/resources/dir.descriptor/out"));
99100

100101
assertNotNull(descriptor);
101102
assertEquals("org.codehaus.plexus.languages.java.demo", descriptor.name());
102-
assertEquals(false, descriptor.isAutomatic());
103+
assertFalse(descriptor.isAutomatic());
103104

104-
assertEquals(3, descriptor.requires().size());
105+
assertThat(descriptor.requires()).hasSize(3);
105106

106107
Set<JavaRequires> expectedRequires = JavaModuleDescriptor.newAutomaticModule("_")
107108
.requires("java.base")
@@ -113,19 +114,21 @@ public void testOutputDirectoryDescriptor() throws Exception {
113114
assertEquals(expectedRequires, descriptor.requires());
114115
}
115116

116-
@Test(expected = NoSuchFileException.class)
117-
public void testClassicOutputDirectory() throws Exception {
118-
parser.getModuleDescriptor(Paths.get("src/test/resources/dir.empty/out"));
117+
@Test
118+
void testClassicOutputDirectory() {
119+
assertThrows(
120+
NoSuchFileException.class,
121+
() -> parser.getModuleDescriptor(Paths.get("src/test/resources/dir.empty/out")));
119122
}
120123

121124
@Test
122-
public void testJModDescriptor() throws Exception {
125+
void testJModDescriptor() throws Exception {
123126
JavaModuleDescriptor descriptor = parser.getModuleDescriptor(
124127
Paths.get("src/test/resources/jmod.descriptor/first-jmod-1.0-SNAPSHOT.jmod"));
125128

126129
assertNotNull(descriptor);
127130
assertEquals("com.corporate.project", descriptor.name());
128-
assertEquals(false, descriptor.isAutomatic());
131+
assertFalse(descriptor.isAutomatic());
129132

130133
assertEquals(1, descriptor.requires().size());
131134
assertEquals("java.base", descriptor.requires().iterator().next().name());
@@ -135,13 +138,14 @@ public void testJModDescriptor() throws Exception {
135138
"com.corporate.project", descriptor.exports().iterator().next().source());
136139
}
137140

138-
@Test(expected = IOException.class)
139-
public void testInvalidFile() throws Exception {
140-
parser.getModuleDescriptor(Paths.get("src/test/resources/nonjar/pom.xml"));
141+
@Test
142+
void testInvalidFile() {
143+
assertThrows(
144+
IOException.class, () -> parser.getModuleDescriptor(Paths.get("src/test/resources/nonjar/pom.xml")));
141145
}
142146

143147
@Test
144-
public void testUses() throws Exception {
148+
void testUses() throws Exception {
145149
try (InputStream is =
146150
Files.newInputStream(Paths.get("src/test/resources/dir.descriptor.uses/out/module-info.class"))) {
147151
JavaModuleDescriptor descriptor = parser.parse(is);
@@ -157,7 +161,7 @@ public void testUses() throws Exception {
157161
}
158162

159163
@Test
160-
public void testProvides() throws Exception {
164+
void testProvides() throws Exception {
161165
JavaModuleDescriptor descriptor =
162166
parser.getModuleDescriptor(Paths.get("src/test/resources/jar.service/threeten-extra-1.4.jar"));
163167

@@ -182,21 +186,21 @@ public void testProvides() throws Exception {
182186
}
183187

184188
@Test
185-
public void testRequires() throws Exception {
189+
void testRequires() throws Exception {
186190
try (InputStream is =
187191
Files.newInputStream(Paths.get("src/test/resources/dir.descriptor.requires/out/module-info.class"))) {
188192
JavaModuleDescriptor descriptor = parser.parse(is);
189193

190194
assertNotNull(descriptor);
191-
assertThat(descriptor.requires().size(), is(5));
195+
assertThat(descriptor.requires()).hasSize(5);
192196

193197
Set<JavaRequires> expectedRequires = JavaModuleDescriptor.newAutomaticModule("_")
194198
.requires("java.base")
195199
.requires("mod_r")
196200
.requires(Collections.singleton(JavaRequires.JavaModifier.STATIC), "mod_r_s")
197201
.requires(Collections.singleton(JavaRequires.JavaModifier.TRANSITIVE), "mod_r_t")
198202
.requires(
199-
new HashSet<JavaRequires.JavaModifier>(Arrays.asList(
203+
new HashSet<>(Arrays.asList(
200204
JavaRequires.JavaModifier.STATIC, JavaRequires.JavaModifier.TRANSITIVE)),
201205
"mod_r_s_t")
202206
.build()

plexus-java/src/test/java/org/codehaus/plexus/languages/java/jpms/CmdModuleNameExtractorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121

2222
import java.nio.file.Path;
2323

24-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2525

26-
import static org.junit.Assert.assertEquals;
26+
import static org.assertj.core.api.Assertions.assertThat;
2727

28-
public class CmdModuleNameExtractorTest {
28+
class CmdModuleNameExtractorTest {
2929
@Test
30-
public void testMethodCount() throws Exception {
30+
void testMethodCount() throws Exception {
3131
// ensure that both implementations are in sync
32-
assertEquals(2, CmdModuleNameExtractor.class.getDeclaredMethods().length);
32+
assertThat(CmdModuleNameExtractor.class.getDeclaredMethods().length).isEqualTo(2);
3333

3434
// if these don't exist, a NoSuchMethodException is thrown
3535
CmdModuleNameExtractor.class.getDeclaredMethod("main", String[].class);

0 commit comments

Comments
 (0)