-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMyExceptionTest.java
More file actions
65 lines (54 loc) · 1.69 KB
/
Copy pathMyExceptionTest.java
File metadata and controls
65 lines (54 loc) · 1.69 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
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.*;
/**
* Test class for MyException
*/
public class MyExceptionTest {
@Test
public void testMyExceptionCreation() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
MyException exception = new MyException();
System.setOut(originalOut);
assertNotNull(exception);
assertTrue(outContent.toString().contains("MyException.MyException"));
}
@Test
public void testMyExceptionExtendsException() {
MyException exception = new MyException();
assertTrue(exception instanceof Exception);
}
@Test
public void testMyExceptionIsThrowable() {
MyException exception = new MyException();
assertTrue(exception instanceof Throwable);
}
@Test(expected = MyException.class)
public void testMyExceptionCanBeThrown() throws MyException {
throw new MyException();
}
@Test
public void testMyExceptionStackTrace() {
MyException exception = new MyException();
assertNotNull(exception.getStackTrace());
assertTrue(exception.getStackTrace().length > 0);
}
@Test
public void testMyExceptionCause() {
MyException exception = new MyException();
assertNull(exception.getCause());
}
@Test
public void testMyExceptionInTryCatch() {
boolean caught = false;
try {
throw new MyException();
} catch (MyException e) {
caught = true;
}
assertTrue(caught);
}
}