Test Driven Development and JUnit


Published on

I just tried to explain TDD through JUnit

Published in: Technology, Education
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Test Driven Development and JUnit

  1. 1. Test Driven Development vis-à-vis JUnit by Somenath Mukhopadhyay [email_address] Somenath Mukhopadhyay
  2. 2. What does it mean? <ul><ul><li>Principle: Clean Code that works </li></ul></ul><ul><ul><li>Purpose: </li></ul></ul><ul><ul><ul><li>Removes fear factors from programmers </li></ul></ul></ul><ul><ul><ul><li>Depicts clearly what the code is doing </li></ul></ul></ul><ul><ul><ul><li>Programmers don’t have to wait for the long trails of bugs and fixing of those bugs </li></ul></ul></ul>Somenath Mukhopadhyay
  3. 3. Three Steps in TDD <ul><ul><li>RED – a failed test case </li></ul></ul><ul><ul><li>GREEN – the test case just passes </li></ul></ul><ul><ul><li>REFACTOR – change the code to meet good design principles </li></ul></ul>Somenath Mukhopadhyay
  4. 4. Example <ul><ul><li>We need to create a Book class </li></ul></ul><ul><ul><li>The Book class will have Author’s name </li></ul></ul><ul><ul><li>It will have Price </li></ul></ul><ul><ul><li>It will have setter and getter functions to access its attributes </li></ul></ul><ul><ul><li>It will have equal functionality to compare two books </li></ul></ul><ul><ul><li>It will have functionality to calculate its price in different currencies </li></ul></ul>Somenath Mukhopadhyay
  5. 5. Various steps of a TDD <ul><ul><li>Quickly add a test </li></ul></ul><ul><ul><li>Run the test case to see it fail </li></ul></ul><ul><ul><li>Make a little change to see the test pass </li></ul></ul><ul><ul><li>Refactor the code to suit the specific need </li></ul></ul>Somenath Mukhopadhyay
  6. 6. Quickly add a test <ul><ul><li>Book book1 </li></ul></ul><ul><ul><li>book1.setAuthor(“Som”)‏ </li></ul></ul><ul><ul><li>book1.setPrice(10.11)‏ </li></ul></ul><ul><ul><li>assertTrue(book1.getAuthor().equals(“Som”))‏ </li></ul></ul><ul><ul><li>assertTrue(book1.getPrice() == 10.11)‏ </li></ul></ul>Somenath Mukhopadhyay
  7. 7. Run the Test <ul><ul><li>Ii will obviously fail </li></ul></ul><ul><ul><li>It is the “ RED ” stage in JUnit </li></ul></ul><ul><ul><li>No Book class has been defined </li></ul></ul>Somenath Mukhopadhyay
  8. 8. Make a little change to see the test Pass <ul><ul><li>Create a Class Book having two attributes </li></ul></ul><ul><ul><li>public class Book { </li></ul></ul><ul><li>string iAuthor; </li></ul><ul><li>double iPrice; </li></ul><ul><li>Book(); </li></ul><ul><li>public void setAuthor(string aAuthor)‏ </li></ul><ul><li>{ </li></ul><ul><li>iAuthor = aAuthor; </li></ul><ul><li>} </li></ul><ul><li>public void setPrice(double aPrice)‏ </li></ul><ul><li>{ </li></ul><ul><li>iPrice = aPrice; </li></ul><ul><li>} </li></ul><ul><li>string getAuthor()‏ </li></ul><ul><li>{ </li></ul><ul><li>return iAuthor; </li></ul><ul><li>} </li></ul><ul><li>double getPrice()‏ </li></ul><ul><li>{ </li></ul><ul><li>return iPrice; </li></ul><ul><li>} </li></ul><ul><ul><li>}; </li></ul></ul>Somenath Mukhopadhyay
  9. 9. Run the Test again <ul><ul><li>With the Book class in hand the test will pass the first hurdle </li></ul></ul><ul><ul><li>It is a GREEN state in JUnit </li></ul></ul>Somenath Mukhopadhyay
  10. 10. Refactor the Code <ul><ul><li>We will create two constructors: </li></ul></ul><ul><li>Book() {this.Author = NULL, this.Price = 0.0)‏ </li></ul><ul><li>Book (string Author, double Price)‏ </li></ul><ul><ul><li>We will make the attributes private </li></ul></ul>Somenath Mukhopadhyay
  11. 11. Refactoring contd… run the test again… <ul><ul><li>Book book2(“reema”, 20.33)‏ </li></ul></ul><ul><ul><li>Book book3(“som”, 10.11)‏ </li></ul></ul><ul><ul><li>assertFalse(book1.equals(book2))‏ </li></ul></ul><ul><ul><li>assertTrue(book1.equals(book3))‏ </li></ul></ul>Somenath Mukhopadhyay
  12. 12. The test will fail again <ul><ul><li>There is no equals functionality defined </li></ul></ul><ul><ul><li>Lets Refactor it again and define it right away </li></ul></ul><ul><ul><li>public boolean equals(Object object) { </li></ul></ul><ul><ul><li>if (object instanceof Book) { </li></ul></ul><ul><ul><li>Book book = (Book) object; </li></ul></ul><ul><ul><li>return (getAuthor().equals(book. getAuthor())‏ </li></ul></ul><ul><ul><li>&& getPrice() == book.getPrice()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>return false ; </li></ul></ul><ul><ul><li>} </li></ul></ul>Somenath Mukhopadhyay
  13. 13. Run the test again <ul><ul><li>The test will pass now. </li></ul></ul><ul><ul><li>It is the GREEN state </li></ul></ul>Somenath Mukhopadhyay
  14. 14. Refactor.. Refactor.. <ul><ul><li>What is left is the currency issue </li></ul></ul><ul><ul><li>So we will add another private string parameter called currency in the book class </li></ul></ul>Somenath Mukhopadhyay
  15. 15. Refactor… Refactor… <ul><ul><li>The constructor of the Book class will now take three parameters </li></ul></ul><ul><ul><li>Book (string Author, double price, string currency)‏ </li></ul></ul><ul><ul><li>We need to construct the objects as </li></ul></ul><ul><li>Book book1(“Som”,12.12, “Yen”)‏ </li></ul><ul><li>Book book2(“Reema”, 13.13, “Rupees”)‏ </li></ul>Somenath Mukhopadhyay
  16. 16. Refactor… Refactor… <ul><ul><li>So we are left with only the convertibility of price between different currencies. </li></ul></ul><ul><ul><li>We can add this function inside Book class </li></ul></ul><ul><ul><li>Hold on! Should this function be Book’s responsibility. </li></ul></ul><ul><ul><li>We can have a modular design if we create a new Currency class </li></ul></ul>Somenath Mukhopadhyay
  17. 17. Refactor… Refactor… <ul><ul><li>Public class Currency { </li></ul></ul><ul><ul><li>private String country; </li></ul></ul><ul><ul><li>Public Currency() {this.currency = “USA”} </li></ul></ul><ul><ul><li>Public Currency(string country)‏ </li></ul></ul><ul><ul><li>Public void setCountry(string c)‏ </li></ul></ul><ul><ul><li>Public string getCountry(); </li></ul></ul><ul><ul><li>public void Convert (Currency to); </li></ul></ul><ul><ul><li>} </li></ul></ul>Somenath Mukhopadhyay
  18. 18. Finally <ul><ul><li>So finally the Book class will look as follows </li></ul></ul><ul><ul><li>public class Book { </li></ul></ul><ul><ul><li>private string Author; </li></ul></ul><ul><ul><li>private double price; </li></ul></ul><ul><ul><li>private Currency currency; </li></ul></ul><ul><ul><li>Public Book (String Author, double price, Currency c)‏ </li></ul></ul><ul><ul><li>public void setAuthor (string Author)‏ </li></ul></ul><ul><ul><li>Public void setPrice (double Price)‏ </li></ul></ul><ul><ul><li>Public void setCurrency(Currency c)‏ </li></ul></ul><ul><ul><li>Public string getAuthor(); </li></ul></ul><ul><ul><li>Public double getPrice(); </li></ul></ul><ul><ul><li>Public Currency getCurrency(); </li></ul></ul><ul><ul><li>Public booloean equals (Object book) </li></ul></ul><ul><ul><li>} </li></ul></ul>Somenath Mukhopadhyay
  19. 19. Refactoring.. Final touch to the Equals function <ul><ul><li>public boolean equals(Object object) { </li></ul></ul><ul><ul><li>if (object instanceof Book) { </li></ul></ul><ul><ul><li>Book book = (Book) object; </li></ul></ul><ul><ul><li>return (getAuthor().equals(book. getAuthor())‏ </li></ul></ul><ul><ul><li>&& getPrice() == book.getPrice() && getCurrency().equals (book.getCurrency()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>return false ; </li></ul></ul><ul><ul><li>} </li></ul></ul>Somenath Mukhopadhyay
  20. 20. Run the test again <ul><ul><li>It will again fail </li></ul></ul><ul><ul><li>We didn’t define the equals function for the currency class </li></ul></ul><ul><ul><li>The equals function of the currency class will look as follows: </li></ul></ul><ul><ul><li>public bool equals(Object c)‏ </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>If (c instanceof Currency)‏ </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>return( getCurrency().getCountry().equals(c.getCurrency().getCountry())‏ </li></ul></ul><ul><ul><li>} </li></ul></ul>Somenath Mukhopadhyay
  21. 21. The Refactoring is Over <ul><ul><li>The test case will pass now </li></ul></ul><ul><ul><li>It will be a “ GREEN” state </li></ul></ul><ul><ul><li>I will leave the Convert Function of the Currency class for you to implement </li></ul></ul>Somenath Mukhopadhyay
  22. 22. The JUnit Code import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite;   public class BookTest extends TestCase {     private Book book1; public BookTest(String name) {         super(name);     } protected void setUp() throws Exception {         super.setUp();                 book1 = new Book(&quot;Som&quot;, 10.11);         book2 = new Book(&quot;Reema&quot;, 12.33);         book3 = new Book(&quot;Som&quot;, 10.11);     }                                                                                                    Contd..... Somenath Mukhopadhyay
  23. 23. The JUnit Code protected void tearDown() throws Exception {         super.tearDown();         book1 = null;         book2 = null;         book3 = null;     }   public void testEquals() {        assertFalse(book2.equalsBook(book1));         assertTrue(book1.equalsBook(book1));         assertTrue(book1.equalsBook(book3));     }   public void testGetPrice() {         double price = book1.getPrice();         assertTrue(price == 10.11);     }                                                                                                        Contd... Somenath Mukhopadhyay
  24. 24. The JUnit Code public void testSetPrice() {         book1.setPrice(15.15);         double price = book1.getPrice();         assertTrue(price == 15.15);     }   public void testGetTitle() {        String title = book2.getTitle();         boolean result = title.equals(&quot;Reema&quot;);         assertTrue(result == true);     }   public static Test suite(){           TestSuite suite = new TestSuite();           suite.addTest(new BookTest(&quot;testEquals&quot;));           suite.addTest(new BookTest(&quot;testGetTitle&quot;));           suite.addTest(new BookTest(&quot;testSetPrice&quot;));         return suite;     } Somenath Mukhopadhyay
  25. 25. Thank You