forked from hmkcode/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppTest.java
More file actions
59 lines (41 loc) · 1.48 KB
/
AppTest.java
File metadata and controls
59 lines (41 loc) · 1.48 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
package com.hmkcode;
import java.util.LinkedList;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.hamcrest.CoreMatchers.is;
@RunWith(JUnit4.class)
public class AppTest
{
// ( 1 ) Simple way to test exception
@Test(expected= NullPointerException.class)
public void testNull() {
String str = null;
str.toUpperCase();
}
// ( 2 ) Test by tyr/catch (test exception message)
@Test
public void testExceptionMessage(){
try{
new LinkedList<Object>().get(0);
//if no exception thrown the test will fail with the below message.
fail("Expected an IndexOutOfBoundsException to be thrown");
} catch (IndexOutOfBoundsException anIndexOutOfBoundsException) {
//if no exception message is not the same the test will fail with the below message.
assertThat(anIndexOutOfBoundsException.getMessage(), is("Index: 0, Size: 0"));
}
}
//( 3 ) Test by @Rule & ExpectedException.expect() | ExpectedException.expectMessage()
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testExceptionByRule() throws ArithmeticException {
thrown.expect(ArithmeticException.class);
thrown.expectMessage("/ by zero");
double d = 1 / 0;
}
}