Skip to content

Commit 945208d

Browse files
Migrate tests to JUnit5 (assertThrows)
* Replace try / fail(expected exception) / catch pattern with assertThrows * Reduce usage of fail()
1 parent df8ff76 commit 945208d

File tree

14 files changed

+141
-193
lines changed

14 files changed

+141
-193
lines changed

ant/src/test/java/org/owasp/dependencycheck/taskdefs/DependencyCheckTaskIT.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import static org.junit.jupiter.api.Assertions.assertThrows;
3131
import static org.junit.jupiter.api.Assertions.assertTrue;
32-
import static org.junit.jupiter.api.Assertions.fail;
3332

3433
/**
3534
*
@@ -127,12 +126,11 @@ void testNestedReportFormat() throws Exception {
127126

128127
@Test
129128
void testNestedBADReportFormat() {
130-
try {
131-
buildFileRule.executeTarget("test.formatBADNested");
132-
fail("Should have had a buildExceotion for a bad format attribute");
133-
} catch (BuildException e) {
134-
assertTrue(e.getMessage().contains("BAD is not a legal value for this attribute"), "Message did not have BAD, unexpected exception: " + e.getMessage());
135-
}
129+
BuildException e = assertThrows(BuildException.class,
130+
() -> buildFileRule.executeTarget("test.formatBADNested"),
131+
"Should have had a buildException for a bad format attribute");
132+
assertTrue(e.getMessage().contains("BAD is not a legal value for this attribute"),
133+
"Message did not have BAD, unexpected exception: " + e.getMessage());
136134
}
137135

138136
/**

cli/src/test/java/org/owasp/dependencycheck/CliParserTest.java

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.junit.jupiter.api.Assertions.assertEquals;
3131
import static org.junit.jupiter.api.Assertions.assertFalse;
3232
import static org.junit.jupiter.api.Assertions.assertNull;
33+
import static org.junit.jupiter.api.Assertions.assertThrows;
3334
import static org.junit.jupiter.api.Assertions.assertTrue;
3435
import static org.junit.jupiter.api.Assertions.fail;
3536

@@ -99,20 +100,17 @@ void testParse_version() throws Exception {
99100
/**
100101
* Test of parse method with failOnCVSS without an argument
101102
*
102-
* @throws Exception thrown when an exception occurs.
103103
*/
104104
@Test
105-
void testParse_failOnCVSSNoArg() throws Exception {
105+
void testParse_failOnCVSSNoArg() {
106106

107107
String[] args = {"--failOnCVSS"};
108108

109109
CliParser instance = new CliParser(getSettings());
110-
try {
111-
instance.parse(args);
112-
fail("an argument for failOnCVSS was missing and an exception was not thrown");
113-
} catch (ParseException ex) {
114-
assertTrue(ex.getMessage().contains("Missing argument"));
115-
}
110+
ParseException ex = assertThrows(ParseException.class, () -> instance.parse(args),
111+
"an argument for failOnCVSS was missing and an exception was not thrown");
112+
assertTrue(ex.getMessage().contains("Missing argument"));
113+
116114
assertFalse(instance.isGetVersion());
117115
assertFalse(instance.isGetHelp());
118116
assertFalse(instance.isRunScan());
@@ -159,10 +157,9 @@ void testParse_failOnCVSSValidArgument() throws Exception {
159157
/**
160158
* Test of parse method with jar and cpe args, of class CliParser.
161159
*
162-
* @throws Exception thrown when an exception occurs.
163160
*/
164161
@Test
165-
void testParse_unknown() throws Exception {
162+
void testParse_unknown() {
166163

167164
String[] args = {"-unknown"};
168165

@@ -173,12 +170,10 @@ void testParse_unknown() throws Exception {
173170

174171
CliParser instance = new CliParser(getSettings());
175172

176-
try {
177-
instance.parse(args);
178-
fail("Unrecognized option should have caused an exception");
179-
} catch (ParseException ex) {
180-
assertTrue(ex.getMessage().contains("Unrecognized option"));
181-
}
173+
ParseException ex = assertThrows(ParseException.class, () -> instance.parse(args) ,
174+
"Unrecognized option should have caused an exception");
175+
assertTrue(ex.getMessage().contains("Unrecognized option"));
176+
182177
assertFalse(instance.isGetVersion());
183178
assertFalse(instance.isGetHelp());
184179
assertFalse(instance.isRunScan());
@@ -187,21 +182,17 @@ void testParse_unknown() throws Exception {
187182
/**
188183
* Test of parse method with scan arg, of class CliParser.
189184
*
190-
* @throws Exception thrown when an exception occurs.
191185
*/
192186
@Test
193-
void testParse_scan() throws Exception {
187+
void testParse_scan() {
194188

195189
String[] args = {"-scan"};
196190

197191
CliParser instance = new CliParser(getSettings());
198192

199-
try {
200-
instance.parse(args);
201-
fail("Missing argument should have caused an exception");
202-
} catch (ParseException ex) {
203-
assertTrue(ex.getMessage().contains("Missing argument"));
204-
}
193+
ParseException ex = assertThrows(ParseException.class, () -> instance.parse(args),
194+
"Missing argument should have caused an exception");
195+
assertTrue(ex.getMessage().contains("Missing argument"));
205196

206197
assertFalse(instance.isGetVersion());
207198
assertFalse(instance.isGetHelp());
@@ -211,20 +202,17 @@ void testParse_scan() throws Exception {
211202
/**
212203
* Test of parse method with jar arg, of class CliParser.
213204
*
214-
* @throws Exception thrown when an exception occurs.
215205
*/
216206
@Test
217-
void testParse_scan_unknownFile() throws Exception {
207+
void testParse_scan_unknownFile() {
218208

219209
String[] args = {"-scan", "jar.that.does.not.exist", "--project", "test"};
220210

221211
CliParser instance = new CliParser(getSettings());
222-
try {
223-
instance.parse(args);
224-
fail("An exception should have been thrown");
225-
} catch (FileNotFoundException ex) {
226-
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
227-
}
212+
213+
FileNotFoundException ex = assertThrows(FileNotFoundException.class, () -> instance.parse(args),
214+
"An exception should have been thrown");
215+
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
228216

229217
assertFalse(instance.isGetVersion());
230218
assertFalse(instance.isGetHelp());
@@ -274,7 +262,7 @@ void testParse_printVersionInfo() {
274262
assertFalse(text.contains("unknown"));
275263
} catch (IOException ex) {
276264
System.setOut(out);
277-
fail("CliParser.printVersionInfo did not write anything to system.out.");
265+
fail("CliParser.printVersionInfo did not write anything to system.out.", ex);
278266
} finally {
279267
System.setOut(out);
280268
}
@@ -318,16 +306,15 @@ void testParse_printHelp() throws Exception {
318306
* Test of getBooleanArgument method, of class CliParser.
319307
*/
320308
@Test
321-
void testGetBooleanArgument() throws ParseException {
309+
void testGetBooleanArgument() {
322310
String[] args = {"--scan", "missing.file", "--artifactoryUseProxy", "false", "--artifactoryParallelAnalysis", "true", "--project", "test"};
323311

324312
CliParser instance = new CliParser(getSettings());
325-
try {
326-
instance.parse(args);
327-
fail("invalid scan should have caused an error");
328-
} catch (FileNotFoundException ex) {
329-
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
330-
}
313+
314+
FileNotFoundException ex = assertThrows(FileNotFoundException.class, () -> instance.parse(args),
315+
"invalid scan should have caused an error");
316+
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
317+
331318
boolean expResult;
332319
Boolean result = instance.getBooleanArgument("missingArgument");
333320
assertNull(result);
@@ -344,17 +331,16 @@ void testGetBooleanArgument() throws ParseException {
344331
* Test of getStringArgument method, of class CliParser.
345332
*/
346333
@Test
347-
void testGetStringArgument() throws ParseException {
334+
void testGetStringArgument() {
348335

349336
String[] args = {"--scan", "missing.file", "--artifactoryUsername", "blue42", "--project", "test"};
350337

351338
CliParser instance = new CliParser(getSettings());
352-
try {
353-
instance.parse(args);
354-
fail("invalid scan argument should have caused an exception");
355-
} catch (FileNotFoundException ex) {
356-
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
357-
}
339+
340+
FileNotFoundException ex = assertThrows(FileNotFoundException.class, () -> instance.parse(args),
341+
"invalid scan argument should have caused an exception");
342+
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
343+
358344
String expResult;
359345
String result = instance.getStringArgument("missingArgument");
360346
assertNull(result);
@@ -365,17 +351,15 @@ void testGetStringArgument() throws ParseException {
365351
}
366352

367353
@Test
368-
void testHasOption() throws ParseException {
354+
void testHasOption() {
369355

370356
String[] args = {"--scan", "missing.file", "--artifactoryUsername", "blue42", "--project", "test"};
371357

372358
CliParser instance = new CliParser(getSettings());
373-
try {
374-
instance.parse(args);
375-
fail("invalid scan argument should have caused an exception");
376-
} catch (FileNotFoundException ex) {
377-
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
378-
}
359+
360+
FileNotFoundException ex = assertThrows(FileNotFoundException.class, () -> instance.parse(args),
361+
"invalid scan argument should have caused an exception");
362+
assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
379363

380364
Boolean result = instance.hasOption("missingOption");
381365
assertNull(result);

core/src/test/java/org/owasp/dependencycheck/EngineIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import java.util.concurrent.Executors;
3737

3838
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
import static org.junit.jupiter.api.Assertions.assertThrows;
3940
import static org.junit.jupiter.api.Assertions.assertTrue;
40-
import static org.junit.jupiter.api.Assertions.fail;
4141
import static org.mockito.Mockito.doReturn;
4242
import static org.mockito.Mockito.doThrow;
4343
import static org.mockito.Mockito.spy;
@@ -72,9 +72,9 @@ void exceptionDuringAnalysisTaskExecutionIsFatal() throws DatabaseException {
7272
when(instance.getExecutorService(analyzer)).thenReturn(executorService);
7373
doReturn(failingAnalysisTask).when(instance).getAnalysisTasks(analyzer, exceptions);
7474

75-
instance.executeAnalysisTasks(analyzer, exceptions);
76-
fail("ExceptionCollection exception was expected");
77-
} catch (ExceptionCollection expected) {
75+
ExceptionCollection expected = assertThrows(ExceptionCollection.class,
76+
() -> instance.executeAnalysisTasks(analyzer, exceptions),
77+
"ExceptionCollection exception was expected");
7878
List<Throwable> collected = expected.getExceptions();
7979
assertEquals(1, collected.size());
8080
assertEquals(java.util.concurrent.ExecutionException.class, collected.get(0).getClass());

core/src/test/java/org/owasp/dependencycheck/analyzer/ArchiveAnalyzerIT.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.owasp.dependencycheck.BaseTest;
2323
import org.owasp.dependencycheck.Engine;
2424
import org.owasp.dependencycheck.dependency.Dependency;
25-
import org.owasp.dependencycheck.exception.InitializationException;
2625
import org.owasp.dependencycheck.utils.Settings;
2726

2827
import java.io.File;
@@ -34,7 +33,6 @@
3433
import static org.junit.jupiter.api.Assertions.assertFalse;
3534
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3635
import static org.junit.jupiter.api.Assertions.assertTrue;
37-
import static org.junit.jupiter.api.Assertions.fail;
3836

3937
/**
4038
*
@@ -113,9 +111,7 @@ void testInitialize() {
113111
try {
114112
instance.setEnabled(true);
115113
instance.setFilesMatched(true);
116-
instance.prepare(null);
117-
} catch (InitializationException ex) {
118-
fail(ex.getMessage());
114+
assertDoesNotThrow(() -> instance.prepare(null));
119115
} finally {
120116
assertDoesNotThrow(instance::close);
121117
}

core/src/test/java/org/owasp/dependencycheck/analyzer/AssemblyAnalyzerTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
import java.io.File;
3636

3737
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
3839
import static org.junit.jupiter.api.Assertions.assertTrue;
39-
import static org.junit.jupiter.api.Assertions.fail;
4040
import static org.junit.jupiter.api.Assumptions.assumeFalse;
4141
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4242

@@ -129,9 +129,8 @@ void testNonexistent() {
129129
Dependency d = new Dependency(test);
130130

131131
try {
132-
analyzer.analyze(d, null);
133-
fail("Expected an AnalysisException");
134-
} catch (AnalysisException ae) {
132+
AnalysisException ae = assertThrows(AnalysisException.class, () -> analyzer.analyze(d, null),
133+
"Expected an AnalysisException");
135134
assertTrue(ae.getMessage().contains("nonexistent.dll does not exist and cannot be analyzed by dependency-check"));
136135
} finally {
137136
System.setProperty(LOG_KEY, oldProp);
@@ -159,9 +158,9 @@ void testWithSettingMono() {
159158
AssemblyAnalyzer aanalyzer = new AssemblyAnalyzer();
160159
aanalyzer.initialize(getSettings());
161160
aanalyzer.accept(new File("test.dll")); // trick into "thinking it is active"
162-
aanalyzer.prepare(null);
163-
fail("Expected an InitializationException");
164-
} catch (InitializationException ae) {
161+
162+
InitializationException ae = assertThrows(InitializationException.class, () -> aanalyzer.prepare(null),
163+
"Expected an InitializationException");
165164
assertEquals("An error occurred with the .NET AssemblyAnalyzer, is the dotnet 8.0 runtime or sdk installed?", ae.getMessage());
166165
} finally {
167166
System.setProperty(LOG_KEY, oldProp);

core/src/test/java/org/owasp/dependencycheck/analyzer/CPEAnalyzerIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private void callDetermineCPE_full(String depName, String expResult, CPEAnalyzer
159159
}
160160
assertTrue(found, "Match not found: { dep:'" + dep.getFileName() + "', exp:'" + expResult + "' }");
161161
} else {
162-
dep.getVulnerableSoftwareIdentifiers().forEach((id) -> fail("Unexpected match found: { dep:'" + dep.getFileName() + "', found:'" + id + "' }"));
162+
dep.getVulnerableSoftwareIdentifiers().forEach(id -> fail("Unexpected match found: { dep:'" + dep.getFileName() + "', found:'" + id + "' }"));
163163
}
164164
}
165165

@@ -222,7 +222,7 @@ void testDetermineCPE() throws Exception {
222222
instance.close();
223223

224224
suppressionAnalyzer.analyze(commonValidator, engine);
225-
commonValidator.getVulnerableSoftwareIdentifiers().forEach((i) -> fail("Apache Common Validator found an unexpected CPE identifier - " + i.getValue()));
225+
commonValidator.getVulnerableSoftwareIdentifiers().forEach(i -> fail("Apache Common Validator found an unexpected CPE identifier - " + i.getValue()));
226226

227227
String expResult = "cpe:2.3:a:apache:struts:2.1.2:*:*:*:*:*:*:*";
228228
assertFalse(struts.getVulnerableSoftwareIdentifiers().isEmpty(), "Incorrect match size - struts");

core/src/test/java/org/owasp/dependencycheck/analyzer/OssIndexAnalyzerTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.util.concurrent.Executors;
2424
import java.util.concurrent.Future;
2525

26+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2627
import static org.junit.jupiter.api.Assertions.assertEquals;
2728
import static org.junit.jupiter.api.Assertions.assertTrue;
28-
import static org.junit.jupiter.api.Assertions.fail;
2929

3030
class OssIndexAnalyzerTest extends BaseTest {
3131

@@ -141,9 +141,8 @@ void should_analyzeDependency_only_warn_when_transport_error_from_sonatype() thr
141141
// When
142142
try (engine) {
143143
engine.setDependencies(Collections.singletonList(dependency));
144-
analyzer.analyzeDependency(dependency, engine);
145-
} catch (AnalysisException e) {
146-
fail("Analysis exception thrown upon remote error although only a warning should have been logged");
144+
assertDoesNotThrow(() -> analyzer.analyzeDependency(dependency, engine),
145+
"Analysis exception thrown upon remote error although only a warning should have been logged");
147146
} finally {
148147
analyzer.close();
149148
}
@@ -169,9 +168,8 @@ void should_analyzeDependency_only_warn_when_socket_error_from_sonatype() throws
169168
// When
170169
try (engine) {
171170
engine.setDependencies(Collections.singletonList(dependency));
172-
analyzer.analyzeDependency(dependency, engine);
173-
} catch (AnalysisException e) {
174-
fail("Analysis exception thrown upon remote error although only a warning should have been logged");
171+
assertDoesNotThrow(() -> analyzer.analyzeDependency(dependency, engine),
172+
"Analysis exception thrown upon remote error although only a warning should have been logged");
175173
} finally {
176174
analyzer.close();
177175
}

core/src/test/java/org/owasp/dependencycheck/data/artifactory/ArtifactorySearchIT.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.List;
2929

3030
import static org.junit.jupiter.api.Assertions.assertEquals;
31-
import static org.junit.jupiter.api.Assertions.fail;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
3232

3333
@Disabled
3434
class ArtifactorySearchIT {
@@ -63,7 +63,7 @@ void testWithRealInstanceUsingBearerToken() throws IOException {
6363
}
6464

6565
@Test
66-
void testWithRealInstanceAnonymous() throws IOException {
66+
void testWithRealInstanceAnonymous() {
6767
// Given
6868
Dependency dependency = new Dependency();
6969
dependency.setSha1sum("c5b4c491aecb72e7c32a78da0b5c6b9cda8dee0f");
@@ -72,13 +72,10 @@ void testWithRealInstanceAnonymous() throws IOException {
7272
settings.setString(Settings.KEYS.ANALYZER_ARTIFACTORY_URL, "https://artifactory.techno.ingenico.com/artifactory");
7373
final ArtifactorySearch artifactorySearch = new ArtifactorySearch(settings);
7474
// When
75-
try {
76-
artifactorySearch.search(dependency);
77-
fail("No Match found, should throw an exception!");
78-
} catch (FileNotFoundException e) {
79-
// Then
80-
assertEquals("Artifact Dependency{ fileName='null', actualFilePath='null', filePath='null', packagePath='null'} not found in Artifactory", e.getMessage());
81-
}
75+
FileNotFoundException e = assertThrows(FileNotFoundException.class, () -> artifactorySearch.search(dependency),
76+
"No Match found, should throw an exception!");
77+
// Then
78+
assertEquals("Artifact Dependency{ fileName='null', actualFilePath='null', filePath='null', packagePath='null'} not found in Artifactory", e.getMessage());
8279
}
8380

8481
@Test

0 commit comments

Comments
 (0)