Test Driven Development and JUnit

50 %
50 %
Information about Test Driven Development and JUnit
Technology

Published on November 10, 2008

Author: som.mukhopadhyay

Source: slideshare.net

Description

I just tried to explain TDD through JUnit

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

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

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

RED – a failed test case

GREEN – the test case just passes

REFACTOR – change the code to meet good design principles

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

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

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

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

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

Book book1

book1.setAuthor(“Som”)‏

book1.setPrice(10.11)‏

assertTrue(book1.getAuthor().equals(“Som”))‏

assertTrue(book1.getPrice() == 10.11)‏

Run the Test Ii will obviously fail It is the “ RED ” stage in JUnit No Book class has been defined Somenath Mukhopadhyay

Ii will obviously fail

It is the “ RED ” stage in JUnit

No Book class has been defined

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

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;

}

};

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

With the Book class in hand the test will pass the first hurdle

It is a GREEN state in JUnit

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

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

  • Add a comment

    Related presentations

    Related pages

    Test-driven development - Wikipedia, the free encyclopedia

    Test-driven development is related to the test-first programming concepts of extreme programming, begun in 1999, [3] but more recently has created more ...
    Read more

    Test Driven Development (TDD): Best Practices Using Java ...

    8 thoughts on “ Test Driven Development (TDD): Best Practices Using Java Examples ” Dhiraj December 28, 2013 at 1:03 am. Comprehensive article.
    Read more

    JUnit - About

    JUnit is a simple framework to write repeatable tests. ... @Test public void lookupEmailAddresses() { assertThat (new ...
    Read more

    JUnit - Wikipedia, the free encyclopedia

    JUnit; Developer(s) Kent Beck, Erich Gamma, David Saff, Mike Clark ... JUnit has been important in the development of test-driven development, ...
    Read more

    JUnit Tutorial - Tutorials for Wordpress, Knowledge ...

    JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of ...
    Read more

    Test Infected: - JUnit

    A JUnit test method is an ordinary method without any ... public static Test suite() ... and less stressed developer. Once you've been test infected, ...
    Read more

    Junit: Test-Driven Development | What Is JUnit? | InformIT

    JUnit is an open source, regression-testing framework that lets Java developers write unit tests as they develop systems. Jasmit Kochhar shows you how to ...
    Read more

    Testgetriebene Entwicklung – Wikipedia

    test first development oder test-driven development (TDD)) ... Bei der Java-Entwicklung kommen dafür meist Ant oder Maven und JUnit zum Einsatz.
    Read more

    tdd - Difference Between Unit Testing and Test Driven ...

    Difference Between Unit Testing and Test Driven Development. ... Test driven development approach is to write unit ... The JUnit tests for JUnit itself ...
    Read more