-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMyRuntimeExceptionTest.java
More file actions
78 lines (65 loc) · 2.28 KB
/
Copy pathMyRuntimeExceptionTest.java
File metadata and controls
78 lines (65 loc) · 2.28 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.*;
/**
* Test class for MyRuntimeException
*/
public class MyRuntimeExceptionTest {
@Test
public void testMyRuntimeExceptionCreation() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
MyRuntimeException exception = new MyRuntimeException();
System.setOut(originalOut);
assertNotNull(exception);
assertTrue(outContent.toString().contains("MyRuntimeException.MyRuntimeException"));
}
@Test
public void testMyRuntimeExceptionExtendsRuntimeException() {
MyRuntimeException exception = new MyRuntimeException();
assertTrue(exception instanceof RuntimeException);
}
@Test
public void testMyRuntimeExceptionIsException() {
MyRuntimeException exception = new MyRuntimeException();
assertTrue(exception instanceof Exception);
}
@Test
public void testMyRuntimeExceptionIsThrowable() {
MyRuntimeException exception = new MyRuntimeException();
assertTrue(exception instanceof Throwable);
}
@Test(expected = MyRuntimeException.class)
public void testMyRuntimeExceptionCanBeThrown() {
throw new MyRuntimeException();
}
@Test
public void testMyRuntimeExceptionStackTrace() {
MyRuntimeException exception = new MyRuntimeException();
assertNotNull(exception.getStackTrace());
assertTrue(exception.getStackTrace().length > 0);
}
@Test
public void testMyRuntimeExceptionCause() {
MyRuntimeException exception = new MyRuntimeException();
assertNull(exception.getCause());
}
@Test
public void testMyRuntimeExceptionInTryCatch() {
boolean caught = false;
try {
throw new MyRuntimeException();
} catch (MyRuntimeException e) {
caught = true;
}
assertTrue(caught);
}
@Test
public void testMyRuntimeExceptionMessage() {
MyRuntimeException exception = new MyRuntimeException();
// Message should be null as constructor doesn't set one
assertNull(exception.getMessage());
}
}