7. How we did it // ShoppingCart.java public class ShoppingCart{ public void add(Product p, int quantity){ ... } public void remove(Product p, int quantity){ ... } } // Tomato.java public class Tomato implements Product{ ... }
8. Add Test in JUnit 3.x import junit.framework.TestCase; public class ShoppingCartTest extends TestCase { private ShoppingCart cart; public void testAdd (){ cart = new ShoppingCart(); Tomato t = new Tomato(); cart.add(t, 10); // Product and quantity assertEquals (cart.count(), 10); } }
9. Add Test in JUnit 4.x import org.junit.Test; import static org.junit.Assert.*; public class ShoppingCartTest{ private ShoppingCart cart; @Test public void testAdd() { cart = new ShoppingCart(); Tomato t = new Tomato(); cart.add(t, 10); assertEquals (cart.count(), 10); } }
10. Remove Test // Add @Test annotation for JUnit 4.x public void testRemove(){ cart = new ShoppingCart(); Tomato t = new Tomato(); cart.add(t, 10); cart.remove(t, 6); assertEquals(cart.count(), 4); }
11. Test Class public class ShoppingCartTest ... { private ShoppingCart cart; public void testAdd(){ cart = new ShoppingCart(); ... } public void testRemove(){ cart = new ShoppingCart(); ... } } Duplication!
13. Lifecycle 4.x @ Before @Test XXX1 @ After @ Before @Test XXX2 @ After @ Before @Test XXX3 @ After
14. Final Test Class public class ShoppingCartTest ... { ... // @org.junit.Before to be added for JUnit 4.x protected void setUp (){ cart = new ShoppingCart(); } // @org.junit.After to be added for JUnit 4.x protected void tearDown (){ cart = null; } // test* methods follow }