Skip to content

Commit 48f3d4e

Browse files
authored
fix(test): fix test assertions (#197)
* Add message to `assertThrows` * Remove `fail` which could never be reached * Replace `assertThrows` with `assertThrowsExactly`
1 parent 8b24237 commit 48f3d4e

File tree

2 files changed

+23
-72
lines changed

2 files changed

+23
-72
lines changed

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
package com.github.packageurl;
2323

2424
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;
25+
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
2726

2827
import java.util.Collections;
2928
import java.util.HashMap;
@@ -110,61 +109,56 @@ void packageURLBuilderException1Null() throws MalformedPackageURLException {
110109

111110
@Test
112111
void packageURLBuilderException2() {
113-
assertThrows(MalformedPackageURLException.class, () -> {
112+
assertThrowsExactly(MalformedPackageURLException.class, () -> {
114113
PackageURLBuilder.aPackageURL()
115114
.withType("type")
116115
.withNamespace("invalid//namespace")
117116
.withName("name")
118117
.build();
119-
fail("Build should fail due to invalid namespace");
120-
});
118+
}, "Build should fail due to invalid namespace");
121119
}
122120

123121
@Test
124122
void packageURLBuilderException3() {
125-
assertThrows(MalformedPackageURLException.class, () -> {
123+
assertThrowsExactly(MalformedPackageURLException.class, () -> {
126124
PackageURLBuilder.aPackageURL()
127125
.withType("typ^e")
128126
.withSubpath("invalid/name%2Fspace")
129127
.withName("name")
130128
.build();
131-
fail("Build should fail due to invalid subpath");
132-
});
129+
}, "Build should fail due to invalid subpath");
133130
}
134131

135132
@Test
136133
void packageURLBuilderException4() {
137-
assertThrows(MalformedPackageURLException.class, () -> {
134+
assertThrowsExactly(MalformedPackageURLException.class, () -> {
138135
PackageURLBuilder.aPackageURL()
139136
.withType("0_type")
140137
.withName("name")
141138
.build();
142-
fail("Build should fail due to invalid type");
143-
});
139+
}, "Build should fail due to invalid type");
144140
}
145141

146142
@Test
147143
void packageURLBuilderException5() {
148-
assertThrows(MalformedPackageURLException.class, () -> {
144+
assertThrowsExactly(MalformedPackageURLException.class, () -> {
149145
PackageURLBuilder.aPackageURL()
150146
.withType("ype")
151147
.withName("name")
152148
.withQualifier("0_key", "value")
153149
.build();
154-
fail("Build should fail due to invalid qualifier key");
155-
});
150+
}, "Build should fail due to invalid qualifier key");
156151
}
157152

158153
@Test
159154
void packageURLBuilderException6() {
160-
assertThrows(MalformedPackageURLException.class, () -> {
155+
assertThrowsExactly(MalformedPackageURLException.class, () -> {
161156
PackageURLBuilder.aPackageURL()
162157
.withType("ype")
163158
.withName("name")
164159
.withQualifier("", "value")
165160
.build();
166-
fail("Build should fail due to invalid qualifier key");
167-
});
161+
}, "Build should fail due to invalid qualifier key");
168162
}
169163

170164
@Test
@@ -186,7 +180,6 @@ void editBuilder1() throws MalformedPackageURLException {
186180
.withoutQualifier("dark");
187181

188182
assertBuilderMatch(new PackageURL("pkg:maven/org.junit/junit5@3.1.2?repo=maven&ping=pong#sub"), b);
189-
190183
}
191184

192185
@Test

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

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static org.junit.jupiter.api.Assertions.assertEquals;
2525
import static org.junit.jupiter.api.Assertions.assertNotNull;
2626
import static org.junit.jupiter.api.Assertions.assertNull;
27-
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
2828
import static org.junit.jupiter.api.Assertions.assertTrue;
2929
import static org.junit.jupiter.api.Assertions.fail;
3030

@@ -229,100 +229,58 @@ void constructor() throws MalformedPackageURLException {
229229

230230
@Test
231231
void constructorWithEmptyType() {
232-
assertThrows(MalformedPackageURLException.class, () -> {
233-
234-
PackageURL purl = new PackageURL("", "name");
235-
fail("constructor with an empty type should have thrown an error and this line should not be reached");
236-
});
232+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("", "name"), "constructor with an empty type should have thrown an error and this line should not be reached");
237233
}
238234

239235
@Test
240236
void constructorWithInvalidCharsType() {
241-
assertThrows(MalformedPackageURLException.class, () -> {
242-
243-
PackageURL purl = new PackageURL("invalid^type", "name");
244-
fail("constructor with `invalid^type` should have thrown an error and this line should not be reached");
245-
});
237+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("invalid^type", "name"), "constructor with `invalid^type` should have thrown an error and this line should not be reached");
246238
}
247239

248240
@Test
249241
void constructorWithInvalidNumberType() {
250-
assertThrows(MalformedPackageURLException.class, () -> {
251-
252-
PackageURL purl = new PackageURL("0invalid", "name");
253-
fail("constructor with `0invalid` should have thrown an error and this line should not be reached");
254-
});
242+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("0invalid", "name"), "constructor with `0invalid` should have thrown an error and this line should not be reached");
255243
}
256244

257245
@Test
258246
void constructorWithInvalidSubpath() {
259-
assertThrows(MalformedPackageURLException.class, () -> {
260-
261-
PackageURL purl = new PackageURL("pkg:GOLANG/google.golang.org/genproto@abcdedf#invalid/%2F/subpath");
262-
fail("constructor with `invalid/%2F/subpath` should have thrown an error and this line should not be reached");
263-
});
247+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg:GOLANG/google.golang.org/genproto@abcdedf#invalid/%2F/subpath"), "constructor with `invalid/%2F/subpath` should have thrown an error and this line should not be reached");
264248
}
265249

266250

267251
@Test
268252
void constructorWithNullPurl() {
269-
assertThrows(NullPointerException.class, () ->
270-
new PackageURL(null),
271-
"constructor with null purl should have thrown an error and this line should not be reached");
253+
assertThrowsExactly(NullPointerException.class, () -> new PackageURL(null), "constructor with null purl should have thrown an error and this line should not be reached");
272254
}
273255

274256
@Test
275257
void constructorWithEmptyPurl() {
276-
assertThrows(MalformedPackageURLException.class, () -> {
277-
278-
PackageURL purl = new PackageURL("");
279-
fail("constructor with empty purl should have thrown an error and this line should not be reached");
280-
});
258+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL(""), "constructor with empty purl should have thrown an error and this line should not be reached");
281259
}
282260

283261
@Test
284262
void constructorWithPortNumber() {
285-
assertThrows(MalformedPackageURLException.class, () -> {
286-
287-
PackageURL purl = new PackageURL("pkg://generic:8080/name");
288-
fail("constructor with port number should have thrown an error and this line should not be reached");
289-
});
263+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg://generic:8080/name"), "constructor with port number should have thrown an error and this line should not be reached");
290264
}
291265

292266
@Test
293267
void constructorWithUsername() {
294-
assertThrows(MalformedPackageURLException.class, () -> {
295-
296-
PackageURL purl = new PackageURL("pkg://user@generic/name");
297-
fail("constructor with username should have thrown an error and this line should not be reached");
298-
});
268+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg://user@generic/name"), "constructor with username should have thrown an error and this line should not be reached");
299269
}
300270

301271
@Test
302272
void constructorWithInvalidUrl() {
303-
assertThrows(MalformedPackageURLException.class, () -> {
304-
305-
PackageURL purl = new PackageURL("invalid url");
306-
fail("constructor with invalid url should have thrown an error and this line should not be reached");
307-
});
273+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("invalid url"), "constructor with invalid url should have thrown an error and this line should not be reached");
308274
}
309275

310276
@Test
311277
void constructorWithDuplicateQualifiers() {
312-
assertThrows(MalformedPackageURLException.class, () -> {
313-
314-
PackageURL purl = new PackageURL("pkg://generic/name?key=one&key=two");
315-
fail("constructor with url with duplicate qualifiers should have thrown an error and this line should not be reached");
316-
});
278+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg://generic/name?key=one&key=two"), "constructor with url with duplicate qualifiers should have thrown an error and this line should not be reached");
317279
}
318280

319281
@Test
320282
void constructorDuplicateQualifiersMixedCase() {
321-
assertThrows(MalformedPackageURLException.class, () -> {
322-
323-
PackageURL purl = new PackageURL("pkg://generic/name?key=one&KEY=two");
324-
fail("constructor with url with duplicate qualifiers should have thrown an error and this line should not be reached");
325-
});
283+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg://generic/name?key=one&KEY=two"), "constructor with url with duplicate qualifiers should have thrown an error and this line should not be reached");
326284
}
327285

328286
@Test

0 commit comments

Comments
 (0)