Skip to content

Commit 0e86301

Browse files
committed
JUnit5 assertThrows TypeHandlerTest
1 parent d4da1bb commit 0e86301

1 file changed

Lines changed: 93 additions & 27 deletions

File tree

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

Lines changed: 93 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;
@@ -26,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2627
import java.net.URL;
2728

2829
import org.junit.Test;
30+
import org.junit.jupiter.api.function.Executable;
2931

3032
public class TypeHandlerTest {
3133

@@ -43,14 +45,28 @@ public void testCreateValueClass() throws Exception {
4345
assertEquals(Instantiable.class, clazz);
4446
}
4547

46-
@Test(expected = ParseException.class)
47-
public void testCreateValueClass_notFound() throws Exception {
48-
TypeHandler.createValue("what ever", PatternOptionBuilder.CLASS_VALUE);
48+
@Test
49+
public void testCreateValueClass_notFound() {
50+
// FIXME Simplification once upgraded to Java 1.8
51+
final Executable testMethod = new Executable() {
52+
@Override
53+
public void execute() throws Throwable {
54+
TypeHandler.createValue("what ever", PatternOptionBuilder.CLASS_VALUE);
55+
}
56+
};
57+
assertThrows(ParseException.class, testMethod);
4958
}
5059

51-
@Test(expected = UnsupportedOperationException.class)
52-
public void testCreateValueDate() throws Exception {
53-
TypeHandler.createValue("what ever", PatternOptionBuilder.DATE_VALUE);
60+
@Test
61+
public void testCreateValueDate() {
62+
// FIXME Simplification once upgraded to Java 1.8
63+
final Executable testMethod = new Executable() {
64+
@Override
65+
public void execute() throws Throwable {
66+
TypeHandler.createValue("what ever", PatternOptionBuilder.DATE_VALUE);
67+
}
68+
};
69+
assertThrows(UnsupportedOperationException.class, testMethod);
5470
}
5571

5672
@Test
@@ -61,9 +77,16 @@ public void testCreateValueExistingFile() throws Exception {
6177
}
6278
}
6379

64-
@Test(expected = ParseException.class)
65-
public void testCreateValueExistingFile_nonExistingFile() throws Exception {
66-
TypeHandler.createValue("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE);
80+
@Test
81+
public void testCreateValueExistingFile_nonExistingFile() {
82+
// FIXME Simplification once upgraded to Java 1.8
83+
final Executable testMethod = new Executable() {
84+
@Override
85+
public void execute() throws Throwable {
86+
TypeHandler.createValue("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE);
87+
}
88+
};
89+
assertThrows(ParseException.class, testMethod);
6790
}
6891

6992
@Test
@@ -72,14 +95,28 @@ public void testCreateValueFile() throws Exception {
7295
assertEquals("some-file.txt", result.getName());
7396
}
7497

75-
@Test(expected = UnsupportedOperationException.class)
76-
public void testCreateValueFiles() throws Exception {
77-
TypeHandler.createValue("some.files", PatternOptionBuilder.FILES_VALUE);
98+
@Test
99+
public void testCreateValueFiles() {
100+
// FIXME Simplification once upgraded to Java 1.8
101+
final Executable testMethod = new Executable() {
102+
@Override
103+
public void execute() throws Throwable {
104+
TypeHandler.createValue("some.files", PatternOptionBuilder.FILES_VALUE);
105+
}
106+
};
107+
assertThrows(UnsupportedOperationException.class, testMethod);
78108
}
79109

80-
@Test(expected = ParseException.class)
81-
public void testCreateValueInteger_failure() throws Exception {
82-
TypeHandler.createValue("just-a-string", Integer.class);
110+
@Test
111+
public void testCreateValueInteger_failure() {
112+
// FIXME Simplification once upgraded to Java 1.8
113+
final Executable testMethod = new Executable() {
114+
@Override
115+
public void execute() throws Throwable {
116+
TypeHandler.createValue("just-a-string", Integer.class);
117+
}
118+
};
119+
assertThrows(ParseException.class, testMethod);
83120
}
84121

85122
@Test
@@ -92,9 +129,16 @@ public void testCreateValueNumber_Long() throws Exception {
92129
assertEquals(Long.valueOf(15), TypeHandler.createValue("15", PatternOptionBuilder.NUMBER_VALUE));
93130
}
94131

95-
@Test(expected = ParseException.class)
96-
public void testCreateValueNumber_noNumber() throws Exception {
97-
TypeHandler.createValue("not a number", PatternOptionBuilder.NUMBER_VALUE);
132+
@Test
133+
public void testCreateValueNumber_noNumber() {
134+
// FIXME Simplification once upgraded to Java 1.8
135+
final Executable testMethod = new Executable() {
136+
@Override
137+
public void execute() throws Throwable {
138+
TypeHandler.createValue("not a number", PatternOptionBuilder.NUMBER_VALUE);
139+
}
140+
};
141+
assertThrows(ParseException.class, testMethod);
98142
}
99143

100144
@Test
@@ -103,14 +147,28 @@ public void testCreateValueObject_InstantiableClass() throws Exception {
103147
assertTrue(result instanceof Instantiable);
104148
}
105149

106-
@Test(expected = ParseException.class)
107-
public void testCreateValueObject_notInstantiableClass() throws Exception {
108-
TypeHandler.createValue(NotInstantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
150+
@Test
151+
public void testCreateValueObject_notInstantiableClass() {
152+
// FIXME Simplification once upgraded to Java 1.8
153+
final Executable testMethod = new Executable() {
154+
@Override
155+
public void execute() throws Throwable {
156+
TypeHandler.createValue(NotInstantiable.class.getName(), PatternOptionBuilder.OBJECT_VALUE);
157+
}
158+
};
159+
assertThrows(ParseException.class, testMethod);
109160
}
110161

111-
@Test(expected = ParseException.class)
112-
public void testCreateValueObject_unknownClass() throws Exception {
113-
TypeHandler.createValue("unknown", PatternOptionBuilder.OBJECT_VALUE);
162+
@Test
163+
public void testCreateValueObject_unknownClass() {
164+
// FIXME Simplification once upgraded to Java 1.8
165+
final Executable testMethod = new Executable() {
166+
@Override
167+
public void execute() throws Throwable {
168+
TypeHandler.createValue("unknown", PatternOptionBuilder.OBJECT_VALUE);
169+
}
170+
};
171+
assertThrows(ParseException.class, testMethod);
114172
}
115173

116174
@Test
@@ -125,8 +183,16 @@ public void testCreateValueURL() throws Exception {
125183
assertEquals(urlString, result.toString());
126184
}
127185

128-
@Test(expected = ParseException.class)
129-
public void testCreateValueURL_malformed() throws Exception {
130-
TypeHandler.createValue("malformed-url", PatternOptionBuilder.URL_VALUE);
186+
@Test
187+
public void testCreateValueURL_malformed() {
188+
// FIXME Simplification once upgraded to Java 1.8
189+
final Executable testMethod = new Executable() {
190+
@Override
191+
public void execute() throws Throwable {
192+
TypeHandler.createValue("malformed-url", PatternOptionBuilder.URL_VALUE);
193+
}
194+
};
195+
assertThrows(ParseException.class, testMethod);
131196
}
197+
132198
}

0 commit comments

Comments
 (0)