This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new e44b530f Use assertThrows()
e44b530f is described below

commit e44b530f5fa35ec1343f03dac145e72824ce5597
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Oct 19 08:04:20 2024 -0400

    Use assertThrows()
---
 .../org/apache/commons/cli/bug/BugCLI71Test.java   | 12 ++++-----
 .../java/org/apache/commons/cli/bug/BugsTest.java  | 30 +++++-----------------
 2 files changed, 11 insertions(+), 31 deletions(-)

diff --git a/src/test/java/org/apache/commons/cli/bug/BugCLI71Test.java 
b/src/test/java/org/apache/commons/cli/bug/BugCLI71Test.java
index 6abb560d..88d7cbd3 100644
--- a/src/test/java/org/apache/commons/cli/bug/BugCLI71Test.java
+++ b/src/test/java/org/apache/commons/cli/bug/BugCLI71Test.java
@@ -18,6 +18,7 @@
 package org.apache.commons.cli.bug;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.fail;
 
 import org.apache.commons.cli.CommandLine;
@@ -69,13 +70,10 @@ public class BugCLI71Test {
 
     @Test
     public void testLackOfError() throws Exception {
-        final String[] args = {"-k", "-a", "Caesar"};
-        try {
-            parser.parse(options, args);
-            fail("MissingArgumentException expected");
-        } catch (final MissingArgumentException e) {
-            assertEquals("k", e.getOption().getOpt(), "option missing an 
argument");
-        }
+        final String[] args = { "-k", "-a", "Caesar" };
+        final MissingArgumentException e = 
assertThrows(MissingArgumentException.class, () -> parser.parse(options, args));
+        parser.parse(options, args);
+        assertEquals("k", e.getOption().getOpt(), "option missing an 
argument");
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/cli/bug/BugsTest.java 
b/src/test/java/org/apache/commons/cli/bug/BugsTest.java
index d0ba4c0e..3de7befe 100644
--- a/src/test/java/org/apache/commons/cli/bug/BugsTest.java
+++ b/src/test/java/org/apache/commons/cli/bug/BugsTest.java
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
@@ -42,6 +43,7 @@ import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.Parser;
 import org.apache.commons.cli.PosixParser;
+import org.apache.commons.lang3.ArrayUtils;
 import org.junit.jupiter.api.Test;
 
 @SuppressWarnings("deprecation") // tests some deprecated classes
@@ -214,19 +216,10 @@ public class BugsTest {
             .hasArg()
             .create('n');
         //@formatter:on
-
         final String[] args = {"-o", "-n", "newpassword"};
-
         options.addOption(oldpass);
         options.addOption(newpass);
-
-        final Parser parser = new PosixParser();
-
-        try {
-            parser.parse(options, args);
-            fail("MissingArgumentException not caught.");
-        } catch (final MissingArgumentException expected) {
-        }
+        assertThrows(MissingArgumentException.class, () -> new 
PosixParser().parse(options, args));
     }
 
     @Test
@@ -279,21 +272,10 @@ public class BugsTest {
 
         final CommandLineParser parser = new PosixParser();
 
-        String[] args = {};
-        try {
-            parser.parse(opts, args);
-            fail("Expected ParseException");
-        } catch (final ParseException expected) {
-        }
-
-        args = new String[] {"-s"};
-        try {
-            parser.parse(opts, args);
-            fail("Expected ParseException");
-        } catch (final ParseException expected) {
-        }
+        assertThrows(ParseException.class, () -> parser.parse(opts, 
ArrayUtils.EMPTY_STRING_ARRAY));
+        assertThrows(ParseException.class, () -> parser.parse(opts, new 
String[] {"-s"}));
 
-        args = new String[] {"-s", "-l"};
+        String[] args = {"-s", "-l"};
         CommandLine line = parser.parse(opts, args);
         assertNotNull(line);
 

Reply via email to