Test Driven Development and JUnit
Upcoming SlideShare
Loading in...5
×
 

Test Driven Development and JUnit

on

  • 10,219 views

I just tried to explain TDD through JUnit

I just tried to explain TDD through JUnit

Statistics

Views

Total Views
10,219
Views on SlideShare
10,184
Embed Views
35

Actions

Likes
4
Downloads
131
Comments
3

3 Embeds 35

http://www.slideshare.net 32
http://www.linkedin.com 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • venki... i have enabled the download option...
    Are you sure you want to
    Your message goes here
    Processing…
  • try to provide download option
    Are you sure you want to
    Your message goes here
    Processing…
  • good sample code
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Test Driven Development and JUnit Test Driven Development and JUnit Presentation Transcript

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