3030import static org .junit .jupiter .api .Assertions .assertEquals ;
3131import static org .junit .jupiter .api .Assertions .assertFalse ;
3232import static org .junit .jupiter .api .Assertions .assertNull ;
33+ import static org .junit .jupiter .api .Assertions .assertThrows ;
3334import static org .junit .jupiter .api .Assertions .assertTrue ;
3435import 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 );
0 commit comments