際際滷

際際滷Share a Scribd company logo
Unit Testing Subhash Chandran S WizTools.org A Java Programmer's Approach
Testing What the program is supposed to do == What the program actually does
Types Unit Integration / Functional System
Unit Testing, Java Style JUnit TestNG (Will not be discussed in this session)
JUnit Version 3.x Version 4.x  Java 5 and above features
What we want to do? Add Remove
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{ ... }
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); } }
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); } }
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); }
Test Class public class ShoppingCartTest ... { private ShoppingCart cart; public void testAdd(){ cart = new ShoppingCart(); ... } public void testRemove(){ cart = new ShoppingCart(); ... } } Duplication!
Lifecycle 3.x setUp () testXXX1() tearDown () setUp () testXXX2() tearDown () setUp () testXXX3() tearDown ()
Lifecycle 4.x @ Before @Test XXX1 @ After @ Before @Test XXX2 @ After @ Before @Test XXX3 @ After
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 }
Other Assert Methods assertNull(...) assertArrayEquals(..., ...) assertEquals(..., ...) assertSame(..., ...) assertTrue(...) and Not of all the above.
Our Approach (so far)... We created the domain classes then the test cases to unit test them Another approach...
Test Driven Development (TDD) First write the test case Then program domain classes to comply to it What the #@&*?
When programming... Details, details and more implementation details...
What Tests Do? Tests define the behavior Tests define what you want to achieve Tests define what your program should do
When All These Are Defined... Your design is beautiful When dealing with implementation details, you will not loose focus You have a execution environment for running your code
So TDD! And save enough time to enjoy Life!
What we covered? Meaning and types of testing JUnit 3.x and 4.x TDD
Action Item Practice TDD :-) Thanks and Bye! Subhash. subwiz (at) gmail.com

More Related Content

Unit Testing

  • 1. Unit Testing Subhash Chandran S WizTools.org A Java Programmer's Approach
  • 2. Testing What the program is supposed to do == What the program actually does
  • 3. Types Unit Integration / Functional System
  • 4. Unit Testing, Java Style JUnit TestNG (Will not be discussed in this session)
  • 5. JUnit Version 3.x Version 4.x Java 5 and above features
  • 6. What we want to do? Add Remove
  • 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!
  • 12. Lifecycle 3.x setUp () testXXX1() tearDown () setUp () testXXX2() tearDown () setUp () testXXX3() tearDown ()
  • 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 }
  • 15. Other Assert Methods assertNull(...) assertArrayEquals(..., ...) assertEquals(..., ...) assertSame(..., ...) assertTrue(...) and Not of all the above.
  • 16. Our Approach (so far)... We created the domain classes then the test cases to unit test them Another approach...
  • 17. Test Driven Development (TDD) First write the test case Then program domain classes to comply to it What the #@&*?
  • 18. When programming... Details, details and more implementation details...
  • 19. What Tests Do? Tests define the behavior Tests define what you want to achieve Tests define what your program should do
  • 20. When All These Are Defined... Your design is beautiful When dealing with implementation details, you will not loose focus You have a execution environment for running your code
  • 21. So TDD! And save enough time to enjoy Life!
  • 22. What we covered? Meaning and types of testing JUnit 3.x and 4.x TDD
  • 23. Action Item Practice TDD :-) Thanks and Bye! Subhash. subwiz (at) gmail.com