Upload
somenath-mukhopadhyay
View
7.886
Download
2
Tags:
Embed Size (px)
DESCRIPTION
I just tried to explain TDD through JUnit
Citation preview
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
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(“So
m”))• 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 asBook 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 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.....
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...
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; }
Thank You