30
30
import static org .junit .jupiter .api .Assertions .assertEquals ;
31
31
import static org .junit .jupiter .api .Assertions .assertFalse ;
32
32
import static org .junit .jupiter .api .Assertions .assertNull ;
33
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
33
34
import static org .junit .jupiter .api .Assertions .assertTrue ;
34
35
import static org .junit .jupiter .api .Assertions .fail ;
35
36
@@ -99,20 +100,17 @@ void testParse_version() throws Exception {
99
100
/**
100
101
* Test of parse method with failOnCVSS without an argument
101
102
*
102
- * @throws Exception thrown when an exception occurs.
103
103
*/
104
104
@ Test
105
- void testParse_failOnCVSSNoArg () throws Exception {
105
+ void testParse_failOnCVSSNoArg () {
106
106
107
107
String [] args = {"--failOnCVSS" };
108
108
109
109
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
+
116
114
assertFalse (instance .isGetVersion ());
117
115
assertFalse (instance .isGetHelp ());
118
116
assertFalse (instance .isRunScan ());
@@ -159,10 +157,9 @@ void testParse_failOnCVSSValidArgument() throws Exception {
159
157
/**
160
158
* Test of parse method with jar and cpe args, of class CliParser.
161
159
*
162
- * @throws Exception thrown when an exception occurs.
163
160
*/
164
161
@ Test
165
- void testParse_unknown () throws Exception {
162
+ void testParse_unknown () {
166
163
167
164
String [] args = {"-unknown" };
168
165
@@ -173,12 +170,10 @@ void testParse_unknown() throws Exception {
173
170
174
171
CliParser instance = new CliParser (getSettings ());
175
172
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
+
182
177
assertFalse (instance .isGetVersion ());
183
178
assertFalse (instance .isGetHelp ());
184
179
assertFalse (instance .isRunScan ());
@@ -187,21 +182,17 @@ void testParse_unknown() throws Exception {
187
182
/**
188
183
* Test of parse method with scan arg, of class CliParser.
189
184
*
190
- * @throws Exception thrown when an exception occurs.
191
185
*/
192
186
@ Test
193
- void testParse_scan () throws Exception {
187
+ void testParse_scan () {
194
188
195
189
String [] args = {"-scan" };
196
190
197
191
CliParser instance = new CliParser (getSettings ());
198
192
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" ));
205
196
206
197
assertFalse (instance .isGetVersion ());
207
198
assertFalse (instance .isGetHelp ());
@@ -211,20 +202,17 @@ void testParse_scan() throws Exception {
211
202
/**
212
203
* Test of parse method with jar arg, of class CliParser.
213
204
*
214
- * @throws Exception thrown when an exception occurs.
215
205
*/
216
206
@ Test
217
- void testParse_scan_unknownFile () throws Exception {
207
+ void testParse_scan_unknownFile () {
218
208
219
209
String [] args = {"-scan" , "jar.that.does.not.exist" , "--project" , "test" };
220
210
221
211
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" ));
228
216
229
217
assertFalse (instance .isGetVersion ());
230
218
assertFalse (instance .isGetHelp ());
@@ -274,7 +262,7 @@ void testParse_printVersionInfo() {
274
262
assertFalse (text .contains ("unknown" ));
275
263
} catch (IOException ex ) {
276
264
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 );
278
266
} finally {
279
267
System .setOut (out );
280
268
}
@@ -318,16 +306,15 @@ void testParse_printHelp() throws Exception {
318
306
* Test of getBooleanArgument method, of class CliParser.
319
307
*/
320
308
@ Test
321
- void testGetBooleanArgument () throws ParseException {
309
+ void testGetBooleanArgument () {
322
310
String [] args = {"--scan" , "missing.file" , "--artifactoryUseProxy" , "false" , "--artifactoryParallelAnalysis" , "true" , "--project" , "test" };
323
311
324
312
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
+
331
318
boolean expResult ;
332
319
Boolean result = instance .getBooleanArgument ("missingArgument" );
333
320
assertNull (result );
@@ -344,17 +331,16 @@ void testGetBooleanArgument() throws ParseException {
344
331
* Test of getStringArgument method, of class CliParser.
345
332
*/
346
333
@ Test
347
- void testGetStringArgument () throws ParseException {
334
+ void testGetStringArgument () {
348
335
349
336
String [] args = {"--scan" , "missing.file" , "--artifactoryUsername" , "blue42" , "--project" , "test" };
350
337
351
338
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
+
358
344
String expResult ;
359
345
String result = instance .getStringArgument ("missingArgument" );
360
346
assertNull (result );
@@ -365,17 +351,15 @@ void testGetStringArgument() throws ParseException {
365
351
}
366
352
367
353
@ Test
368
- void testHasOption () throws ParseException {
354
+ void testHasOption () {
369
355
370
356
String [] args = {"--scan" , "missing.file" , "--artifactoryUsername" , "blue42" , "--project" , "test" };
371
357
372
358
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" ));
379
363
380
364
Boolean result = instance .hasOption ("missingOption" );
381
365
assertNull (result );
0 commit comments