Skip to content

Commit e44b530

Browse files
committed
Use assertThrows()
1 parent 3896533 commit e44b530

2 files changed

Lines changed: 11 additions & 31 deletions

File tree

src/test/java/org/apache/commons/cli/bug/BugCLI71Test.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.cli.bug;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.fail;
2223

2324
import org.apache.commons.cli.CommandLine;
@@ -69,13 +70,10 @@ public void testGetsDefaultIfOptional() throws Exception {
6970

7071
@Test
7172
public void testLackOfError() throws Exception {
72-
final String[] args = {"-k", "-a", "Caesar"};
73-
try {
74-
parser.parse(options, args);
75-
fail("MissingArgumentException expected");
76-
} catch (final MissingArgumentException e) {
77-
assertEquals("k", e.getOption().getOpt(), "option missing an argument");
78-
}
73+
final String[] args = { "-k", "-a", "Caesar" };
74+
final MissingArgumentException e = assertThrows(MissingArgumentException.class, () -> parser.parse(options, args));
75+
parser.parse(options, args);
76+
assertEquals("k", e.getOption().getOpt(), "option missing an argument");
7977
}
8078

8179
@Test

src/test/java/org/apache/commons/cli/bug/BugsTest.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
2323
import static org.junit.jupiter.api.Assertions.assertNull;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
2526
import static org.junit.jupiter.api.Assertions.fail;
2627

@@ -42,6 +43,7 @@
4243
import org.apache.commons.cli.ParseException;
4344
import org.apache.commons.cli.Parser;
4445
import org.apache.commons.cli.PosixParser;
46+
import org.apache.commons.lang3.ArrayUtils;
4547
import org.junit.jupiter.api.Test;
4648

4749
@SuppressWarnings("deprecation") // tests some deprecated classes
@@ -214,19 +216,10 @@ public void test13425() throws Exception {
214216
.hasArg()
215217
.create('n');
216218
//@formatter:on
217-
218219
final String[] args = {"-o", "-n", "newpassword"};
219-
220220
options.addOption(oldpass);
221221
options.addOption(newpass);
222-
223-
final Parser parser = new PosixParser();
224-
225-
try {
226-
parser.parse(options, args);
227-
fail("MissingArgumentException not caught.");
228-
} catch (final MissingArgumentException expected) {
229-
}
222+
assertThrows(MissingArgumentException.class, () -> new PosixParser().parse(options, args));
230223
}
231224

232225
@Test
@@ -279,21 +272,10 @@ public void test13935() throws Exception {
279272

280273
final CommandLineParser parser = new PosixParser();
281274

282-
String[] args = {};
283-
try {
284-
parser.parse(opts, args);
285-
fail("Expected ParseException");
286-
} catch (final ParseException expected) {
287-
}
288-
289-
args = new String[] {"-s"};
290-
try {
291-
parser.parse(opts, args);
292-
fail("Expected ParseException");
293-
} catch (final ParseException expected) {
294-
}
275+
assertThrows(ParseException.class, () -> parser.parse(opts, ArrayUtils.EMPTY_STRING_ARRAY));
276+
assertThrows(ParseException.class, () -> parser.parse(opts, new String[] {"-s"}));
295277

296-
args = new String[] {"-s", "-l"};
278+
String[] args = {"-s", "-l"};
297279
CommandLine line = parser.parse(opts, args);
298280
assertNotNull(line);
299281

0 commit comments

Comments
 (0)