1- // verifying/JUnitDemo .java
1+ // verifying/SimpleJUnit .java
22// (c)2016 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55// Simple use of JUnit to test ArrayList
66import 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 .*;
119
1210// So we can see the list objects being created,
1311// and keep track of when they are cleaned up:
1412class CountedList extends ArrayList <String > {
1513 private static int counter = 0 ;
1614 private int id = counter ++;
1715 public CountedList () {
18- out .println ("CountedList #" + id );
16+ System . out .println ("CountedList #" + id );
1917 }
2018 public int getId () { return id ; }
2119}
2220
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 ());
2727 for (int i = 0 ; i < 3 ; i ++)
28- list .add ("" + i );
28+ list .add (Integer . toString ( i ) );
2929 }
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 ());
4033 }
4134 // All tests are marked with the @Test annotation:
4235 @ Test
4336 public void insert () {
44- out .println ("Running testInsert()" );
37+ System . out .println ("Running testInsert()" );
4538 assertEquals (list .size (), 3 );
4639 list .add (1 , "Insert" );
4740 assertEquals (list .size (), 4 );
4841 assertEquals (list .get (1 ), "Insert" );
4942 }
5043 @ Test
5144 public void replace () {
52- out .println ("Running testReplace()" );
45+ System . out .println ("Running testReplace()" );
5346 assertEquals (list .size (), 3 );
5447 list .set (1 , "Replace" );
5548 assertEquals (list .size (), 3 );
@@ -68,20 +61,20 @@ void compare(ArrayList<String> lst, String[] strs) {
6861 }
6962 @ Test
7063 public void order () {
71- out .println ("Running testOrder()" );
64+ System . out .println ("Running testOrder()" );
7265 compare (list , new String [] { "0" , "1" , "2" });
7366 }
7467 @ Test
7568 public void remove () {
76- out .println ("Running testRemove()" );
69+ System . out .println ("Running testRemove()" );
7770 assertEquals (list .size (), 3 );
7871 list .remove (1 );
7972 assertEquals (list .size (), 2 );
8073 compare (list , new String [] { "0" , "2" });
8174 }
8275 @ Test
8376 public void addAll () {
84- out .println ("Running testAddAll()" );
77+ System . out .println ("Running testAddAll()" );
8578 list .addAll (Arrays .asList (new String [] {
8679 "An" , "African" , "Swallow" }));
8780 assertEquals (list .size (), 6 );
@@ -91,7 +84,7 @@ public void addAll() {
9184 public static void main (String [] args ) {
9285 // Invoke JUnit on the class:
9386 org .junit .runner .JUnitCore .runClasses (
94- JUnitDemo .class );
87+ SimpleJUnit .class );
9588 }
9689}
9790/* Output:
0 commit comments