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 <ul><li>Unit </li></ul><ul><li>Integration / Functional </li></ul><ul><li>System </li></ul>
Unit Testing, Java Style <ul><li>JUnit </li></ul><ul><li>TestNG (Will not be discussed in this session) </li></ul>
JUnit <ul><li>Version 3.x </li></ul><ul><li>Version 4.x – Java 5 and above features </li></ul>
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 ...
Add Test in JUnit 3.x import junit.framework.TestCase; public class  ShoppingCartTest   extends TestCase { private Shoppin...
Add Test in JUnit 4.x import org.junit.Test; import  static  org.junit.Assert.*; public class ShoppingCartTest{ private Sh...
Remove Test // Add @Test annotation for JUnit 4.x public void testRemove(){ cart = new ShoppingCart(); Tomato t = new Toma...
Test Class public class ShoppingCartTest ... { private ShoppingCart cart; public void testAdd(){ cart = new ShoppingCart()...
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  se...
Other Assert Methods <ul><li>assertNull(...) </li></ul><ul><li>assertArrayEquals(..., ...) </li></ul><ul><li>assertEquals(...
Our Approach (so far)... <ul><li>We created the domain classes </li></ul><ul><li>then </li></ul><ul><li>the test cases to ...
Test Driven Development (TDD) <ul><li>First write the test case </li></ul><ul><li>Then program domain classes to comply to...
When programming... Details, details and more implementation details...
What Tests Do? <ul><li>Tests define the behavior </li></ul><ul><li>Tests define what you want to achieve </li></ul><ul><li...
When All These Are Defined... <ul><li>Your design is beautiful </li></ul><ul><li>When dealing with implementation details,...
So TDD! And save enough time to enjoy Life!
What we covered? <ul><li>Meaning and types of testing </li></ul><ul><li>JUnit 3.x and 4.x </li></ul><ul><li>TDD </li></ul>
Action Item <ul><li>Practice TDD :-) </li></ul>Thanks and Bye! Subhash. subwiz (at) gmail.com
Upcoming SlideShare
Loading in...5
×

Unit Testing

5,741

Published on

Presentation on Unit Testing and Test Driven Development using JUnit.

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,741
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
281
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Unit Testing

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

    Clipping is a handy way to collect important slides you want to go back to later.

×