-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListTestsWIthExceptions.java
More file actions
50 lines (41 loc) · 1.31 KB
/
Copy pathListTestsWIthExceptions.java
File metadata and controls
50 lines (41 loc) · 1.31 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
package junit;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays;
import java.util.List;
import static junit.framework.Assert.assertNull;
import static junit.framework.TestCase.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class ListTestsWIthExceptions {
private List<String> strings = Arrays.asList("this", "is", "a", "list", "of", "strings");
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void defaultListHasSixStrings () {
assertThat(strings.size(), is(6));
}
@Test
public void nullListThrowsNPEOldStyle () {
strings = null;
try {
strings.add("hello");
fail("should have thrown an NPE");
}catch (NullPointerException e) {
assertNull(strings);
}
}
@Test(expected = NullPointerException.class)
public void nullListThrowsNPE () {
strings = null;
strings.add("hello");
}
@Test
public void nullListThrowsNPEUsingRule () {
String[] stringArray = strings.toArray(new String[0]);
thrown.expect(ArrayIndexOutOfBoundsException.class);
thrown.expectMessage("7");
System.out.println(stringArray[7]);
}
}