Skip to content

Commit e3a52b8

Browse files
committed
Fix assertThrows
* Add message to `assertThrows` * Remove `fail` which could never be reached * Replace `assertThrows` with `assertThrowsExactly`
1 parent dd5f743 commit e3a52b8

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

@@ -204,100 +204,58 @@ void constructor() throws MalformedPackageURLException {
204204

205205
@Test
206206
void constructorWithEmptyType() {
207-
assertThrows(MalformedPackageURLException.class, () -> {
208-
209-
PackageURL purl = new PackageURL("", "name");
210-
fail("constructor with an empty type should have thrown an error and this line should not be reached");
211-
});
207+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("", "name"), "constructor with an empty type should have thrown an error and this line should not be reached");
212208
}
213209

214210
@Test
215211
void constructorWithInvalidCharsType() {
216-
assertThrows(MalformedPackageURLException.class, () -> {
217-
218-
PackageURL purl = new PackageURL("invalid^type", "name");
219-
fail("constructor with `invalid^type` should have thrown an error and this line should not be reached");
220-
});
212+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("invalid^type", "name"), "constructor with `invalid^type` should have thrown an error and this line should not be reached");
221213
}
222214

223215
@Test
224216
void constructorWithInvalidNumberType() {
225-
assertThrows(MalformedPackageURLException.class, () -> {
226-
227-
PackageURL purl = new PackageURL("0invalid", "name");
228-
fail("constructor with `0invalid` should have thrown an error and this line should not be reached");
229-
});
217+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("0invalid", "name"), "constructor with `0invalid` should have thrown an error and this line should not be reached");
230218
}
231219

232220
@Test
233221
void constructorWithInvalidSubpath() {
234-
assertThrows(MalformedPackageURLException.class, () -> {
235-
236-
PackageURL purl = new PackageURL("pkg:GOLANG/google.golang.org/genproto@abcdedf#invalid/%2F/subpath");
237-
fail("constructor with `invalid/%2F/subpath` should have thrown an error and this line should not be reached");
238-
});
222+
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");
239223
}
240224

241225

242226
@Test
243227
void constructorWithNullPurl() {
244-
assertThrows(NullPointerException.class, () ->
245-
new PackageURL(null),
246-
"constructor with null purl should have thrown an error and this line should not be reached");
228+
assertThrowsExactly(NullPointerException.class, () -> new PackageURL(null), "constructor with null purl should have thrown an error and this line should not be reached");
247229
}
248230

249231
@Test
250232
void constructorWithEmptyPurl() {
251-
assertThrows(MalformedPackageURLException.class, () -> {
252-
253-
PackageURL purl = new PackageURL("");
254-
fail("constructor with empty purl should have thrown an error and this line should not be reached");
255-
});
233+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL(""), "constructor with empty purl should have thrown an error and this line should not be reached");
256234
}
257235

258236
@Test
259237
void constructorWithPortNumber() {
260-
assertThrows(MalformedPackageURLException.class, () -> {
261-
262-
PackageURL purl = new PackageURL("pkg://generic:8080/name");
263-
fail("constructor with port number should have thrown an error and this line should not be reached");
264-
});
238+
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");
265239
}
266240

267241
@Test
268242
void constructorWithUsername() {
269-
assertThrows(MalformedPackageURLException.class, () -> {
270-
271-
PackageURL purl = new PackageURL("pkg://user@generic/name");
272-
fail("constructor with username should have thrown an error and this line should not be reached");
273-
});
243+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("pkg://user@generic/name"), "constructor with username should have thrown an error and this line should not be reached");
274244
}
275245

276246
@Test
277247
void constructorWithInvalidUrl() {
278-
assertThrows(MalformedPackageURLException.class, () -> {
279-
280-
PackageURL purl = new PackageURL("invalid url");
281-
fail("constructor with invalid url should have thrown an error and this line should not be reached");
282-
});
248+
assertThrowsExactly(MalformedPackageURLException.class, () -> new PackageURL("invalid url"), "constructor with invalid url should have thrown an error and this line should not be reached");
283249
}
284250

285251
@Test
286252
void constructorWithDuplicateQualifiers() {
287-
assertThrows(MalformedPackageURLException.class, () -> {
288-
289-
PackageURL purl = new PackageURL("pkg://generic/name?key=one&key=two");
290-
fail("constructor with url with duplicate qualifiers should have thrown an error and this line should not be reached");
291-
});
253+
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");
292254
}
293255

294256
@Test
295257
void constructorDuplicateQualifiersMixedCase() {
296-
assertThrows(MalformedPackageURLException.class, () -> {
297-
298-
PackageURL purl = new PackageURL("pkg://generic/name?key=one&KEY=two");
299-
fail("constructor with url with duplicate qualifiers should have thrown an error and this line should not be reached");
300-
});
258+
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");
301259
}
302260

303261
@Test

0 commit comments

Comments
 (0)