-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathExceptionsDemoTest.java
More file actions
59 lines (50 loc) · 1.84 KB
/
Copy pathExceptionsDemoTest.java
File metadata and controls
59 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test class for ExceptionsDemo
*/
public class ExceptionsDemoTest {
@Test(expected = exceptions.MyRuntimeException.class)
public void testMainThrowsMyRuntimeException() throws exceptions.MyException {
ExceptionsDemo.main(new String[]{});
}
@Test
public void testExceptionsDemoHasMainMethod() throws NoSuchMethodException {
assertNotNull(ExceptionsDemo.class.getMethod("main", String[].class));
}
@Test
public void testMainMethodThrowsMyException() {
try {
java.lang.reflect.Method method = ExceptionsDemo.class.getMethod("main", String[].class);
Class<?>[] exceptionTypes = method.getExceptionTypes();
boolean throwsMyException = false;
for (Class<?> exceptionType : exceptionTypes) {
if (exceptionType.getName().equals("exceptions.MyException")) {
throwsMyException = true;
break;
}
}
assertTrue(throwsMyException);
} catch (NoSuchMethodException e) {
fail("main method not found");
}
}
@Test
public void testMyRuntimeExceptionWithMessage() {
exceptions.MyRuntimeException exception = new exceptions.MyRuntimeException("xxx");
assertEquals("xxx", exception.getMessage());
}
@Test(expected = exceptions.MyRuntimeException.class)
public void testThrowMyRuntimeException() {
throw new exceptions.MyRuntimeException("test message");
}
@Test(expected = exceptions.MyException.class)
public void testThrowMyException() throws exceptions.MyException {
throw new exceptions.MyException();
}
@Test
public void testConditionalLogic() {
boolean condition = 1 < 2;
assertTrue(condition);
}
}