Top Banner
Somenath Mukhopadhyay Test Driven Development vis-à-vis JUnit by Somenath Mukhopadhyay [email protected]
25

Test Driven Development and JUnit

Jan 18, 2015

Download

Technology

I just tried to explain TDD through JUnit
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Test Driven Development and JUnit

Somenath Mukhopadhyay

Test Driven Development

vis-à-vis JUnit

by Somenath [email protected]

Page 2: Test Driven Development and JUnit

Somenath Mukhopadhyay

What does it mean?

• Principle: Clean Code that works

• Purpose: o Removes fear factors from programmerso Depicts clearly what the code is doingo Programmers don’t have to wait for the long

trails of bugs and fixing of those bugs

Page 3: Test Driven Development and JUnit

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

Page 4: Test Driven Development and JUnit

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

Page 5: Test Driven Development and JUnit

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

Page 6: Test Driven Development and JUnit

Somenath Mukhopadhyay

Quickly add a test

• Book book1• book1.setAuthor(“Som”)• book1.setPrice(10.11)• assertTrue(book1.getAuthor().equals(“So

m”))• assertTrue(book1.getPrice() == 10.11)

Page 7: Test Driven Development and JUnit

Somenath Mukhopadhyay

Run the Test

• Ii will obviously fail

• It is the “RED” stage in JUnit

• No Book class has been defined

Page 8: Test Driven Development and JUnit

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;}

• };

Page 9: Test Driven Development and JUnit

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

Page 10: Test Driven Development and 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

Page 11: Test Driven Development and JUnit

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))

Page 12: Test Driven Development and JUnit

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;• }

Page 13: Test Driven Development and JUnit

Somenath Mukhopadhyay

Run the test again

• The test will pass now.

• It is the GREEN state

Page 14: Test Driven Development and JUnit

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

Page 15: Test Driven Development and JUnit

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 asBook book1(“Som”,12.12, “Yen”)Book book2(“Reema”, 13.13, “Rupees”)

Page 16: Test Driven Development and JUnit

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

Page 17: Test Driven Development and JUnit

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);• }

Page 18: Test Driven Development and JUnit

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) • }

Page 19: Test Driven Development and JUnit

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;• }

Page 20: Test Driven Development and JUnit

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())• }

Page 21: Test Driven Development and JUnit

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

Page 22: Test Driven Development and JUnit

Somenath Mukhopadhyay

The JUnit Codeimport 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.....

Page 23: Test Driven Development and JUnit

Somenath Mukhopadhyay

The JUnit Codeprotected 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...

Page 24: Test Driven Development and JUnit

Somenath Mukhopadhyay

The JUnit Codepublic 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;    }

Page 25: Test Driven Development and JUnit

Thank You