forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJUnitDemo.java
More file actions
109 lines (107 loc) · 3.14 KB
/
JUnitDemo.java
File metadata and controls
109 lines (107 loc) · 3.14 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// unittesting/JUnitDemo.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Simple use of JUnit to test ArrayList
// (Install libraries from junit.org)
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static java.lang.System.out;
// So we can see the list objects being created,
// and keep track of when they are cleaned up:
class CountedList extends ArrayList<String> {
private static int counter = 0;
private int id = counter++;
public CountedList() {
out.println("CountedList #" + id);
}
public int getId() { return id; }
}
public class JUnitDemo {
private CountedList list = new CountedList();
// You can use the constructor instead of setUp():
public JUnitDemo() {
for(int i = 0; i < 3; i++)
list.add("" + i);
}
// Thus, setUp() is optional, but is run right
// before the test:
protected void setUp() {
out.println("Set up for " + list.getId());
}
// tearDown() is also optional, and is called after
// each test. setUp() and tearDown() can be either
// protected or public:
public void tearDown() {
out.println("Tearing down " + list.getId());
}
// All tests are marked with the @Test annotation:
@Test
public void insert() {
out.println("Running testInsert()");
assertEquals(list.size(), 3);
list.add(1, "Insert");
assertEquals(list.size(), 4);
assertEquals(list.get(1), "Insert");
}
@Test
public void replace() {
out.println("Running testReplace()");
assertEquals(list.size(), 3);
list.set(1, "Replace");
assertEquals(list.size(), 3);
assertEquals(list.get(1), "Replace");
}
// A helper method to reduce code duplication. As long
// as it isn't annotated with @Test, it will not
// be automatically executed by JUnit.
private
void compare(ArrayList<String> lst, String[] strs) {
String[] array = (String[])lst.toArray();
assertTrue("Arrays not the same length",
array.length == strs.length);
for(int i = 0; i < array.length; i++)
assertEquals(strs[i], array[i]);
}
@Test
public void order() {
out.println("Running testOrder()");
compare(list, new String[] { "0", "1", "2" });
}
@Test
public void remove() {
out.println("Running testRemove()");
assertEquals(list.size(), 3);
list.remove(1);
assertEquals(list.size(), 2);
compare(list, new String[] { "0", "2" });
}
@Test
public void addAll() {
out.println("Running testAddAll()");
list.addAll(Arrays.asList(new String[] {
"An", "African", "Swallow"}));
assertEquals(list.size(), 6);
compare(list, new String[] { "0", "1", "2",
"An", "African", "Swallow" });
}
public static void main(String[] args) {
// Invoke JUnit on the class:
org.junit.runner.JUnitCore.runClasses(
JUnitDemo.class);
}
}
/* Output:
CountedList #0
Running testAddAll()
CountedList #1
Running testInsert()
CountedList #2
Running testRemove()
CountedList #3
Running testOrder()
CountedList #4
Running testReplace()
*/