Skip to content

Commit dd5f743

Browse files
authored
build: migrates tests to JUnit 5 (#196)
* Apply JUnit 5 Best practices recipe Applies the [OpenRewrite JUnit 5 Best Practices recipe](https://docs.openrewrite.org/recipes/java/testing/junit5/junit5bestpractices): ``` mvn rewrite:run -Dactive.recipes=org.openrewrite.java.testing.junit5.JUnit5BestPractices ``` * Fixes compilation errors
1 parent a52acc2 commit dd5f743

File tree

3 files changed

+250
-234
lines changed

3 files changed

+250
-234
lines changed

pom.xml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
<jacoco.maven.plugin.version>0.8.11</jacoco.maven.plugin.version>
7474
<spotbugs.maven.plugin.version>4.8.3.1</spotbugs.maven.plugin.version>
7575
<com.github.spotbugs.version>4.8.3</com.github.spotbugs.version>
76+
<!-- Dependency versions -->
77+
<junit.version>5.12.0</junit.version>
7678
</properties>
7779

7880
<scm>
@@ -103,6 +105,21 @@
103105
</repository>
104106
</distributionManagement>
105107

108+
<dependencyManagement>
109+
<dependencies>
110+
111+
<!-- BOMs at the end, so they don't overwrite the dependencies above -->
112+
<dependency>
113+
<groupId>org.junit</groupId>
114+
<artifactId>junit-bom</artifactId>
115+
<version>${junit.version}</version>
116+
<type>pom</type>
117+
<scope>import</scope>
118+
</dependency>
119+
120+
</dependencies>
121+
</dependencyManagement>
122+
106123
<dependencies>
107124
<dependency>
108125
<groupId>jakarta.validation</groupId>
@@ -116,18 +133,17 @@
116133
<artifactId>jspecify</artifactId>
117134
<version>1.0.0</version>
118135
</dependency>
119-
<dependency>
120-
<groupId>junit</groupId>
121-
<artifactId>junit</artifactId>
122-
<version>4.13.2</version>
123-
<scope>test</scope>
124-
</dependency>
125136
<dependency>
126137
<groupId>org.json</groupId>
127138
<artifactId>json</artifactId>
128139
<version>20240303</version>
129140
<scope>test</scope>
130141
</dependency>
142+
<dependency>
143+
<groupId>org.junit.jupiter</groupId>
144+
<artifactId>junit-jupiter-api</artifactId>
145+
<scope>test</scope>
146+
</dependency>
131147
</dependencies>
132148

133149
<build>

src/test/java/com/github/packageurl/PackageURLBuilderTest.java

Lines changed: 65 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,19 @@
2121
*/
2222
package com.github.packageurl;
2323

24-
import org.hamcrest.CoreMatchers;
25-
import org.hamcrest.MatcherAssert;
26-
import org.junit.Assert;
27-
import org.junit.Rule;
28-
import org.junit.Test;
29-
import org.junit.rules.ExpectedException;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
import static org.junit.jupiter.api.Assertions.fail;
3027

3128
import java.util.Collections;
3229
import java.util.HashMap;
3330
import java.util.Map;
31+
import org.junit.jupiter.api.Test;
3432

35-
import static org.hamcrest.CoreMatchers.allOf;
36-
import static org.hamcrest.CoreMatchers.containsString;
37-
import static org.junit.Assert.*;
38-
39-
public class PackageURLBuilderTest {
40-
41-
@Rule
42-
public ExpectedException exception = ExpectedException.none();
33+
class PackageURLBuilderTest {
4334

4435
@Test
45-
public void testPackageURLBuilder() throws MalformedPackageURLException {
46-
exception = ExpectedException.none();
47-
36+
void packageURLBuilder() throws MalformedPackageURLException {
4837
PackageURL purl = PackageURLBuilder.aPackageURL()
4938
.withType("my.type-9+")
5039
.withName("name")
@@ -100,81 +89,86 @@ public void testPackageURLBuilder() throws MalformedPackageURLException {
10089
}
10190

10291
@Test
103-
public void testPackageURLBuilderException1() throws MalformedPackageURLException {
92+
void packageURLBuilderException1() throws MalformedPackageURLException {
10493
PackageURL purl = PackageURLBuilder.aPackageURL()
10594
.withType("type")
10695
.withName("name")
10796
.withQualifier("key","")
10897
.build();
109-
assertEquals("qualifier count", 0, purl.getQualifiers().size());
98+
assertEquals(0, purl.getQualifiers().size(), "qualifier count");
11099
}
111100

112101
@Test
113-
public void testPackageURLBuilderException1Null() throws MalformedPackageURLException {
102+
void packageURLBuilderException1Null() throws MalformedPackageURLException {
114103
PackageURL purl = PackageURLBuilder.aPackageURL()
115104
.withType("type")
116105
.withName("name")
117106
.withQualifier("key",null)
118107
.build();
119-
assertEquals("qualifier count", 0, purl.getQualifiers().size());
108+
assertEquals(0, purl.getQualifiers().size(), "qualifier count");
120109
}
121110

122111
@Test
123-
public void testPackageURLBuilderException2() throws MalformedPackageURLException {
124-
exception.expect(MalformedPackageURLException.class);
125-
PackageURLBuilder.aPackageURL()
126-
.withType("type")
127-
.withNamespace("invalid//namespace")
128-
.withName("name")
129-
.build();
130-
Assert.fail("Build should fail due to invalid namespace");
112+
void packageURLBuilderException2() {
113+
assertThrows(MalformedPackageURLException.class, () -> {
114+
PackageURLBuilder.aPackageURL()
115+
.withType("type")
116+
.withNamespace("invalid//namespace")
117+
.withName("name")
118+
.build();
119+
fail("Build should fail due to invalid namespace");
120+
});
131121
}
132122

133123
@Test
134-
public void testPackageURLBuilderException3() throws MalformedPackageURLException {
135-
exception.expect(MalformedPackageURLException.class);
136-
PackageURLBuilder.aPackageURL()
137-
.withType("typ^e")
138-
.withSubpath("invalid/name%2Fspace")
139-
.withName("name")
140-
.build();
141-
Assert.fail("Build should fail due to invalid subpath");
124+
void packageURLBuilderException3() {
125+
assertThrows(MalformedPackageURLException.class, () -> {
126+
PackageURLBuilder.aPackageURL()
127+
.withType("typ^e")
128+
.withSubpath("invalid/name%2Fspace")
129+
.withName("name")
130+
.build();
131+
fail("Build should fail due to invalid subpath");
132+
});
142133
}
143134

144135
@Test
145-
public void testPackageURLBuilderException4() throws MalformedPackageURLException {
146-
exception.expect(MalformedPackageURLException.class);
147-
PackageURLBuilder.aPackageURL()
148-
.withType("0_type")
149-
.withName("name")
150-
.build();
151-
Assert.fail("Build should fail due to invalid type");
136+
void packageURLBuilderException4() {
137+
assertThrows(MalformedPackageURLException.class, () -> {
138+
PackageURLBuilder.aPackageURL()
139+
.withType("0_type")
140+
.withName("name")
141+
.build();
142+
fail("Build should fail due to invalid type");
143+
});
152144
}
153145

154146
@Test
155-
public void testPackageURLBuilderException5() throws MalformedPackageURLException {
156-
exception.expect(MalformedPackageURLException.class);
157-
PackageURLBuilder.aPackageURL()
158-
.withType("ype")
159-
.withName("name")
160-
.withQualifier("0_key","value")
161-
.build();
162-
Assert.fail("Build should fail due to invalid qualifier key");
147+
void packageURLBuilderException5() {
148+
assertThrows(MalformedPackageURLException.class, () -> {
149+
PackageURLBuilder.aPackageURL()
150+
.withType("ype")
151+
.withName("name")
152+
.withQualifier("0_key", "value")
153+
.build();
154+
fail("Build should fail due to invalid qualifier key");
155+
});
163156
}
164157

165158
@Test
166-
public void testPackageURLBuilderException6() throws MalformedPackageURLException {
167-
exception.expect(MalformedPackageURLException.class);
168-
PackageURLBuilder.aPackageURL()
169-
.withType("ype")
170-
.withName("name")
171-
.withQualifier("","value")
172-
.build();
173-
Assert.fail("Build should fail due to invalid qualifier key");
159+
void packageURLBuilderException6() {
160+
assertThrows(MalformedPackageURLException.class, () -> {
161+
PackageURLBuilder.aPackageURL()
162+
.withType("ype")
163+
.withName("name")
164+
.withQualifier("", "value")
165+
.build();
166+
fail("Build should fail due to invalid qualifier key");
167+
});
174168
}
175169

176170
@Test
177-
public void testEditBuilder1() throws MalformedPackageURLException {
171+
void editBuilder1() throws MalformedPackageURLException {
178172

179173
PackageURL p = new PackageURL("pkg:generic/namespace/[email protected]?k=v#s");
180174
PackageURLBuilder b = p.toBuilder();
@@ -196,7 +190,7 @@ public void testEditBuilder1() throws MalformedPackageURLException {
196190
}
197191

198192
@Test
199-
public void testQualifiers() throws MalformedPackageURLException {
193+
void qualifiers() throws MalformedPackageURLException {
200194
Map<String, String> qualifiers = new HashMap<>();
201195
qualifiers.put("key2", "value2");
202196
Map<String, String> qualifiers2 = new HashMap<>();
@@ -219,7 +213,7 @@ public void testQualifiers() throws MalformedPackageURLException {
219213
}
220214

221215
@Test
222-
public void testFromExistingPurl() throws MalformedPackageURLException {
216+
void fromExistingPurl() throws MalformedPackageURLException {
223217
String purl = "pkg:generic/namespace/[email protected]?k=v#s";
224218
PackageURL p = new PackageURL(purl);
225219
PackageURL purl2 = PackageURLBuilder.aPackageURL(p).build();
@@ -230,22 +224,21 @@ public void testFromExistingPurl() throws MalformedPackageURLException {
230224

231225
private void assertBuilderMatch(PackageURL expected, PackageURLBuilder actual) throws MalformedPackageURLException {
232226

233-
Assert.assertEquals(expected.toString(), actual.build().toString());
234-
Assert.assertEquals(expected.getType(), actual.getType());
235-
Assert.assertEquals(expected.getNamespace(), actual.getNamespace());
236-
Assert.assertEquals(expected.getName(), actual.getName());
237-
Assert.assertEquals(expected.getVersion(), actual.getVersion());
238-
Assert.assertEquals(expected.getSubpath(), actual.getSubpath());
227+
assertEquals(expected.toString(), actual.build().toString());
228+
assertEquals(expected.getType(), actual.getType());
229+
assertEquals(expected.getNamespace(), actual.getNamespace());
230+
assertEquals(expected.getName(), actual.getName());
231+
assertEquals(expected.getVersion(), actual.getVersion());
232+
assertEquals(expected.getSubpath(), actual.getSubpath());
239233

240234
Map<String, String> eQualifiers = expected.getQualifiers();
241235
Map<String, String> aQualifiers = actual.getQualifiers();
242236

243237
assertEquals(eQualifiers, aQualifiers);
244238

245239
if (eQualifiers != null && aQualifiers != null) {
246-
eQualifiers.forEach((k,v) -> {
247-
Assert.assertEquals(v, actual.getQualifier(k));
248-
});
240+
eQualifiers.forEach((k,v) ->
241+
assertEquals(v, actual.getQualifier(k)));
249242
}
250243
}
251244

0 commit comments

Comments
 (0)