Skip to content

Commit c89c385

Browse files
authored
JUnit5 assertThrows TypeHandlerTest (apache#141)
* JUnit5 Api Dependency * JUnit5 assertThrows TypeHandlerTest
1 parent c58d23d commit c89c385

2 files changed

Lines changed: 43 additions & 27 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@
182182
</contributors>
183183

184184
<dependencies>
185+
<dependency>
186+
<groupId>org.junit.jupiter</groupId>
187+
<artifactId>junit-jupiter-api</artifactId>
188+
<scope>test</scope>
189+
</dependency>
185190
<dependency>
186191
<groupId>org.junit.vintage</groupId>
187192
<artifactId>junit-vintage-engine</artifactId>

src/test/java/org/apache/commons/cli/TypeHandlerTest.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.commons.cli;
1919

20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertTrue;
@@ -43,14 +44,16 @@ public void testCreateValueClass() throws Exception {
4344
assertEquals(Instantiable.class, clazz);
4445
}
4546

46-
@Test(expected = ParseException.class)
47-
public void testCreateValueClass_notFound() throws Exception {
48-
TypeHandler.createValue("what ever", PatternOptionBuilder.CLASS_VALUE);
47+
@Test
48+
public void testCreateValueClass_notFound() {
49+
assertThrows(ParseException.class, () ->
50+
TypeHandler.createValue("what ever", PatternOptionBuilder.CLASS_VALUE));
4951
}
5052

51-
@Test(expected = UnsupportedOperationException.class)
52-
public void testCreateValueDate() throws Exception {
53-
TypeHandler.createValue("what ever", PatternOptionBuilder.DATE_VALUE);
53+
@Test
54+
public void testCreateValueDate() {
55+
assertThrows(UnsupportedOperationException.class, () ->
56+
TypeHandler.createValue("what ever", PatternOptionBuilder.DATE_VALUE));
5457
}
5558

5659
@Test
@@ -61,9 +64,10 @@ public void testCreateValueExistingFile() throws Exception {
6164
}
6265
}
6366

64-
@Test(expected = ParseException.class)
65-
public void testCreateValueExistingFile_nonExistingFile() throws Exception {
66-
TypeHandler.createValue("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE);
67+
@Test
68+
public void testCreateValueExistingFile_nonExistingFile() {
69+
assertThrows(ParseException.class, () ->
70+
TypeHandler.createValue("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE));
6771
}
6872

6973
@Test
@@ -72,14 +76,16 @@ public void testCreateValueFile() throws Exception {
7276
assertEquals("some-file.txt", result.getName());
7377
}
7478

75-
@Test(expected = UnsupportedOperationException.class)
76-
public void testCreateValueFiles() throws Exception {
77-
TypeHandler.createValue("some.files", PatternOptionBuilder.FILES_VALUE);
79+
@Test
80+
public void testCreateValueFiles() {
81+
assertThrows(UnsupportedOperationException.class, () ->
82+
TypeHandler.createValue("some.files", PatternOptionBuilder.FILES_VALUE));
7883
}
7984

80-
@Test(expected = ParseException.class)
81-
public void testCreateValueInteger_failure() throws Exception {
82-
TypeHandler.createValue("just-a-string", Integer.class);
85+
@Test
86+
public void testCreateValueInteger_failure() {
87+
assertThrows(ParseException.class, () ->
88+
TypeHandler.createValue("just-a-string", Integer.class));
8389
}
8490

8591
@Test
@@ -92,9 +98,10 @@ public void testCreateValueNumber_Long() throws Exception {
9298
assertEquals(Long.valueOf(15), TypeHandler.createValue("15", PatternOptionBuilder.NUMBER_VALUE));
9399
}
94100

95-
@Test(expected = ParseException.class)
96-
public void testCreateValueNumber_noNumber() throws Exception {
97-
TypeHandler.createValue("not a number", PatternOptionBuilder.NUMBER_VALUE);
101+
@Test
102+
public void testCreateValueNumber_noNumber() {
103+
assertThrows(ParseException.class, () ->
104+
TypeHandler.createValue("not a number", PatternOptionBuilder.NUMBER_VALUE));
98105
}
99106

100107
@Test
@@ -103,14 +110,16 @@ public void testCreateValueObject_InstantiableClass() throws Exception {
103110
assertTrue(result instanceof Instantiable);
104111
}
105112

106-
@Test(expected = ParseException.class)
107-
public void testCreateValueObject_notInstantiableClass() throws Exception {
108-
TypeHandler.createValue(NotInstantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
113+
@Test
114+
public void testCreateValueObject_notInstantiableClass() {
115+
assertThrows(ParseException.class, () ->
116+
TypeHandler.createValue(NotInstantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE));
109117
}
110118

111-
@Test(expected = ParseException.class)
112-
public void testCreateValueObject_unknownClass() throws Exception {
113-
TypeHandler.createValue("unknown", PatternOptionBuilder.OBJECT_VALUE);
119+
@Test
120+
public void testCreateValueObject_unknownClass() {
121+
assertThrows(ParseException.class, () ->
122+
TypeHandler.createValue("unknown", PatternOptionBuilder.OBJECT_VALUE));
114123
}
115124

116125
@Test
@@ -125,8 +134,10 @@ public void testCreateValueURL() throws Exception {
125134
assertEquals(urlString, result.toString());
126135
}
127136

128-
@Test(expected = ParseException.class)
129-
public void testCreateValueURL_malformed() throws Exception {
130-
TypeHandler.createValue("malformed-url", PatternOptionBuilder.URL_VALUE);
137+
@Test
138+
public void testCreateValueURL_malformed() {
139+
assertThrows(ParseException.class, () ->
140+
TypeHandler.createValue("malformed-url", PatternOptionBuilder.URL_VALUE));
131141
}
142+
132143
}

0 commit comments

Comments
 (0)