1
- // verifying/JUnitDemo .java
1
+ // verifying/SimpleJUnit .java
2
2
// (c)2016 MindView LLC: see Copyright.txt
3
3
// We make no guarantees that this code is fit for any purpose.
4
4
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5
5
// Simple use of JUnit to test ArrayList
6
6
import java .util .*;
7
- import org .junit .Test ;
8
- import static org .junit .Assert .assertEquals ;
9
- import static org .junit .Assert .assertTrue ;
10
- import static java .lang .System .out ;
7
+ import org .junit .*;
8
+ import static org .junit .Assert .*;
11
9
12
10
// So we can see the list objects being created,
13
11
// and keep track of when they are cleaned up:
14
12
class CountedList extends ArrayList <String > {
15
13
private static int counter = 0 ;
16
14
private int id = counter ++;
17
15
public CountedList () {
18
- out .println ("CountedList #" + id );
16
+ System . out .println ("CountedList #" + id );
19
17
}
20
18
public int getId () { return id ; }
21
19
}
22
20
23
- public class JUnitDemo {
24
- private CountedList list = new CountedList ();
25
- // You can use the constructor instead of setUp():
26
- public JUnitDemo () {
21
+ public class SimpleJUnit {
22
+ private CountedList list ;
23
+ @ Before
24
+ public void initialize () {
25
+ list = new CountedList ();
26
+ System .out .println ("Set up for " + list .getId ());
27
27
for (int i = 0 ; i < 3 ; i ++)
28
- list .add ("" + i );
28
+ list .add (Integer . toString ( i ) );
29
29
}
30
- // Thus, setUp() is optional, but is run right
31
- // before the test:
32
- protected void setUp () {
33
- out .println ("Set up for " + list .getId ());
34
- }
35
- // tearDown() is also optional, and is called after
36
- // each test. setUp() and tearDown() can be either
37
- // protected or public:
38
- public void tearDown () {
39
- out .println ("Tearing down " + list .getId ());
30
+ @ After
31
+ public void cleanup () {
32
+ System .out .println ("Cleaning up " + list .getId ());
40
33
}
41
34
// All tests are marked with the @Test annotation:
42
35
@ Test
43
36
public void insert () {
44
- out .println ("Running testInsert()" );
37
+ System . out .println ("Running testInsert()" );
45
38
assertEquals (list .size (), 3 );
46
39
list .add (1 , "Insert" );
47
40
assertEquals (list .size (), 4 );
48
41
assertEquals (list .get (1 ), "Insert" );
49
42
}
50
43
@ Test
51
44
public void replace () {
52
- out .println ("Running testReplace()" );
45
+ System . out .println ("Running testReplace()" );
53
46
assertEquals (list .size (), 3 );
54
47
list .set (1 , "Replace" );
55
48
assertEquals (list .size (), 3 );
@@ -68,20 +61,20 @@ void compare(ArrayList<String> lst, String[] strs) {
68
61
}
69
62
@ Test
70
63
public void order () {
71
- out .println ("Running testOrder()" );
64
+ System . out .println ("Running testOrder()" );
72
65
compare (list , new String [] { "0" , "1" , "2" });
73
66
}
74
67
@ Test
75
68
public void remove () {
76
- out .println ("Running testRemove()" );
69
+ System . out .println ("Running testRemove()" );
77
70
assertEquals (list .size (), 3 );
78
71
list .remove (1 );
79
72
assertEquals (list .size (), 2 );
80
73
compare (list , new String [] { "0" , "2" });
81
74
}
82
75
@ Test
83
76
public void addAll () {
84
- out .println ("Running testAddAll()" );
77
+ System . out .println ("Running testAddAll()" );
85
78
list .addAll (Arrays .asList (new String [] {
86
79
"An" , "African" , "Swallow" }));
87
80
assertEquals (list .size (), 6 );
@@ -91,7 +84,7 @@ public void addAll() {
91
84
public static void main (String [] args ) {
92
85
// Invoke JUnit on the class:
93
86
org .junit .runner .JUnitCore .runClasses (
94
- JUnitDemo .class );
87
+ SimpleJUnit .class );
95
88
}
96
89
}
97
90
/* Output:
0 commit comments