OnJava8-Examples/unittesting/JUnitDemo.java

95 lines
2.8 KiB
Java
Raw Normal View History

2015-04-26 13:01:40 -07:00
//: unittesting/JUnitDemo.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-26 13:01:40 -07:00
// Simple use of JUnit to test ArrayList
2015-05-18 23:05:20 -07:00
// (Install libraries from www.junit.org)
2015-04-26 13:01:40 -07:00
import java.util.*;
import org.junit.Test;
2015-05-18 23:05:20 -07:00
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static java.lang.System.out;
2015-04-26 13:01:40 -07:00
// So we can see the list objects being created,
// and keep track of when they are cleaned up:
2015-05-06 15:14:33 -07:00
class CountedList extends ArrayList<String> {
2015-04-26 13:01:40 -07:00
private static int counter = 0;
private int id = counter++;
public CountedList() {
2015-05-18 23:05:20 -07:00
out.println("CountedList #" + id);
2015-04-26 13:01:40 -07:00
}
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() {
2015-05-18 23:05:20 -07:00
out.println("Set up for " + list.getId());
2015-04-26 13:01:40 -07:00
}
// tearDown() is also optional, and is called after
// each test. setUp() and tearDown() can be either
// protected or public:
public void tearDown() {
2015-05-18 23:05:20 -07:00
out.println("Tearing down " + list.getId());
2015-04-26 13:01:40 -07:00
}
2015-05-18 23:05:20 -07:00
// All tests are marked with the @Test annotation:
2015-04-26 13:01:40 -07:00
@Test
2015-05-18 23:05:20 -07:00
public void insert() {
out.println("Running testInsert()");
assertEquals(list.size(), 3);
2015-04-26 13:01:40 -07:00
list.add(1, "Insert");
2015-05-18 23:05:20 -07:00
assertEquals(list.size(), 4);
assertEquals(list.get(1), "Insert");
2015-04-26 13:01:40 -07:00
}
@Test
2015-05-18 23:05:20 -07:00
public void replace() {
out.println("Running testReplace()");
assertEquals(list.size(), 3);
2015-04-26 13:01:40 -07:00
list.set(1, "Replace");
2015-05-18 23:05:20 -07:00
assertEquals(list.size(), 3);
assertEquals(list.get(1), "Replace");
2015-04-26 13:01:40 -07:00
}
2015-05-18 23:05:20 -07:00
// A helper method to reduce code duplication. As long
// as it isn't annotated with @Test, it will not
2015-04-26 13:01:40 -07:00
// be automatically executed by JUnit.
2015-05-18 23:05:20 -07:00
private void compare(ArrayList<String> lst, String[] strs) {
String[] array = (String[])lst.toArray();
assertTrue("Arrays not the same length",
2015-04-26 13:01:40 -07:00
array.length == strs.length);
for(int i = 0; i < array.length; i++)
2015-05-18 23:05:20 -07:00
assertEquals(strs[i], array[i]);
2015-04-26 13:01:40 -07:00
}
@Test
2015-05-18 23:05:20 -07:00
public void order() {
out.println("Running testOrder()");
2015-04-26 13:01:40 -07:00
compare(list, new String[] { "0", "1", "2" });
}
@Test
2015-05-18 23:05:20 -07:00
public void remove() {
out.println("Running testRemove()");
assertEquals(list.size(), 3);
2015-04-26 13:01:40 -07:00
list.remove(1);
2015-05-18 23:05:20 -07:00
assertEquals(list.size(), 2);
2015-04-26 13:01:40 -07:00
compare(list, new String[] { "0", "2" });
}
@Test
2015-05-18 23:05:20 -07:00
public void addAll() {
out.println("Running testAddAll()");
2015-05-06 15:14:33 -07:00
list.addAll(Arrays.asList(new String[] {
2015-04-26 13:01:40 -07:00
"An", "African", "Swallow"}));
2015-05-18 23:05:20 -07:00
assertEquals(list.size(), 6);
2015-04-26 13:01:40 -07:00
compare(list, new String[] { "0", "1", "2",
"An", "African", "Swallow" });
}
public static void main(String[] args) {
// Invoke JUnit on the class:
2015-05-18 23:05:20 -07:00
org.junit.runner.JUnitCore.runClasses(
JUnitDemo.class);
2015-04-26 13:01:40 -07:00
}
} ///:~