2015-09-07 11:44:36 -06:00
|
|
|
// unittesting/JUnitDemo.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
// Simple use of JUnit to test ArrayList
|
|
|
|
// (Install libraries from www.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);
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
CountedList #0
|
|
|
|
Running testAddAll()
|
|
|
|
CountedList #1
|
|
|
|
Running testInsert()
|
|
|
|
CountedList #2
|
|
|
|
Running testRemove()
|
|
|
|
CountedList #3
|
|
|
|
Running testOrder()
|
|
|
|
CountedList #4
|
|
|
|
Running testReplace()
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|