About Me

My photo
"Enoughtheory.com" had its humble beginning in the year 2011 by ( Founder of Enoughtheory.com ) Mr Ravi Kant Soni , an Enterprise Java and Spring Framework Specialist, with a bachelor degree (B.E) in Information Science and Engineering from Reva Institute of Technology at Bangalore. He has been into the software development discipline for many years now. Ravi has worn many hats throughout his tenure, ranging from software development, designing multi-tenant applications, integration of new technology into an existing system, to his current love of writing a Spring Framework book. Currently, he is a lead engineer at HCL Technology. Ravi has focused on Web and Enterprise development using Spring Framework for most of his career and has been extensively involved in application design and implementation. He has developed applications for Core-Bank, HR and Payroll System, and e-Commerce systems using Spring Framework. Ravi Kant Soni is author of book "Learning Spring Application development" http://learningspringapplicationdevelopment.com

Monday 22 October 2012

Eclipse - unit testing with JUnit in Java

JUnit is a simple Java testing framework to write tests for you Java application. 

What is JUnit

JUnit is a simple open source Java testing
framework used to write and run repeatable automated tests. It is an
instance of the xUnit architecture for unit testing framework.
Eclipse supports creating test cases and running test suites, so it
is easy to use for your Java applications.

JUnit features include:
  • Assertions for testing expected results
  • Test fixtures for sharing common test
    data
  • Test suites for easily organizing and
    running tests
  • Graphical and textual test runners

    What is a test case

    A test case is a class which holds a number
    of test methods. For example if you want to test some methods of a
    class Book you create a class BookTest which extends
    the JUnit TestCase class and place your test methods in there.

    How you write and run a simple test


  • Create
    a subclass of TestCase:



public class BookTest extends TestCase{ 
 //..
}





  1. Write
    a test method to assert expected results on the object under test:

Note: The naming convention for a test
method is testXXX()

public void testCollection() {
 Collection collection = new ArrayList();
 assertTrue(collection.isEmpty());
} 





  1. Write
    a suite() method that uses reflection to dynamically create a
    test suite containing all the testXXX() methods:



public static Test suite(){
 return new TestSuite(BookTest.class);
}






  1. Activate the JUnit view in Eclipse
    (Window > Show View > Other.. > Java > JUnit).





You
find the JUnit tab near the Package Explorer tab. You
can change the position of the tab by drag and drop it.





  1. Right click on the subclass of
    TestCase and choose Run > JUnit Test to run the
    test.

Using a test fixture


A test fixture is useful if you have two or
more tests for a common set of objects. Using a test fixture avoids
duplicating the test code necessary to initialize and cleanup those
common objects for each test.

To create a test fixture, define a setUp()
method that initializes common object and a tearDown() method
to cleanup those objects. The JUnit framework automatically invokes
the setUp() method before a each test is run and the
tearDown() method after each test is run.

The following test uses a test fixture:

public class BookTest2 extends TestCase {

    private Collection collection;

    protected void setUp() {
       collection = new ArrayList();
    }

    protected void tearDown() {
  collection.clear(); 
    }

    public void testEmptyCollection(){
  assertTrue(collection.isEmpty());
    }
}


Dynamic and static way of running single tests


JUnit supports two ways (static and dynamic)
of running single tests.
In static way you override the runTest()
method inherited form TestCase class and call the desired test case.
A convenient way to do this is with an anonymous inner class.
Note: Each test must be given a name, so
you can identify it if it fails.
TestCase test = new BookTest("equals test") {<br> public void runTest() {<br>       testEquals();<br>      }<br>};



The dynamic way to create a test case to be
run uses reflection to implement runTest. It assumes the name
of the test is the name of the test case method to invoke. It
dynamically finds and invokes the test method. The dynamic way is
more compact to write but it is less static type safe. An error in
the name of the test case goes unnoticed until you run it and get a
NoSuchMethodException. We leave the choice of which to use up
to you.

TestCast test = new BookTest(“testEquals”);


What is a TestSuite


If you have two tests and you’ll run them
together you could run the tests one at a time yourself, but you
would quickly grow tired of that. Instead, JUnit provides an object
TestSuite which runs any number of test cases together. The
suite method is like a main method that is specialized to run tests.
Create a suite and add each test case you
want to execute:
public static void suite(){<br> TestSuite suite = new TestSuite();<br> suite.addTest(new BookTest("testEquals"));<br> suite.addTest(new BookTest("testBookAdd"));<br> return suite;<br>}



Since JUnit 2.0 there is an even simpler way
to create a test suite, which holds all testXXX() methods. You only
pass the class with the tests to a TestSuite and it extracts the test
methods automatically.

Note: If you use this way to create a
TestSuite all test methods will be added. If you do not want all test
methods in the TestSuite use the normal way to create it.

Example:

public static void suite(){
 return new TestSuite(BookTest.class);
}


A little example


Create a new Java project named
JUnitExample.
Add a package
de.laliluna.tutorial.junitexample where you place the example
classes and a package test.laliluna.tutorial.junitexample
where you place your test classes.

The class Book

Create a new class Book in the
package de.laliluna.tutorial.junitexample.
Add two properties title of type
String and price of type double.
Add a constructor to set the two properties.
Provide a getter- and setter-method for each
of them.
Add a method trunk for a method
equals(Object object) which checks if the object is an
instance of the class Book and the values of the object are equal.
The method return a boolean value.
Note: Do not write the logic of the
equals(..) method, we do it after finish creating the test
method.
The following source code shows the class
Book.
public class Book {<br><br>    private String title;<br>    private double price;<br><br>    /**<br>     * Constructor <br>     * <br>     * @param title<br>     * @param price<br>     */<br>    public Book(String title,<br>                double price) {<br>        this.title = title;<br>        this.price = price;<br>    }<br><br>    /**<br>     * Check if an object is an instance of book<br>     * and the values of title and price are equal<br>     * then return true, otherwise return false<br>     */<br>    public boolean equals(Object object) {<br><br>        return false;<br>    }<br><br>    public double getPrice() {<br>        return price;<br>    }<br><br>    public void setPrice(double price) {<br>        this.price = price;<br>    }<br><br>    public String getTitle() {<br>        return title;<br>    }<br><br>    public void setTitle(String title) {<br>        this.title = title;<br>    }<br>}

The test case BookTest


Create a new test case BookTest in
the package test.laliluna.tutorial.junitexample
Right click on the package and choose
New > JUnit Test
Case
.
In the wizard
choose the methods stubs setUp(), tearDown() and
constructor().





The
following source code shows the class BookTest
public class BookTest extends TestCase {<br><br>    /**<br>     * setUp() method that initializes common objects<br>     */<br>    protected void setUp() throws Exception {<br>        super.setUp();<br>    }<br><br>    /**<br>     * tearDown() method that cleanup the common objects<br>     */<br>    protected void tearDown() throws Exception {<br>        super.tearDown();<br>    }<br><br>    /**<br>     * Constructor for BookTest.<br>     * @param name<br>     */<br>    public BookTest(String name) {<br>        super(name);<br>    }<br><br>}



Now we want to write a test for the
equals(..) method of the class Book. We provide three
private properties, book1, book2 and book3 of type Book.
private Book book1;<br>private Book book2;<br>private Book book3;



Within the setUp() method we
initializes the three properties with some values. Property book1 and
book3 are the same.

protected void setUp() throws Exception {
 super.setUp();
      book1 = new Book(“ES”, 12.99);
      book2 = new Book(“The Gate”, 11.99);
 book3 = new Book(“ES”, 12.99);
}





Within the tearDown() method we cleanup the
properties:

protected void tearDown() throws Exception {
 super.tearDown();
      book1 = null;
      book2 = null;
 book3 = null;
}





Now, add a test method testEquals()
to the test case. Within the method we use the assertFalse()
method of the JUnit framework to test if the return-value of the
equals(..) method is false, because book1 and book2 are not the same.
If the return-value is false the logic of the equals() method is
correct, otherwise there is a logical problem while comparing the
objects. We want to test if the method compares the objects correctly
by using the assertTrue() method. Book1 and Book3 are the same,
because both are an instance of the class Book and have the same
values.

The following source code shows the
testEquals() method:

public void testEquals(){
        assertFalse(book2.equals(book1));
        assertTrue(book1.equals(book1));
}





Writing the logic of the equals() method


We have finished the test and now we can add
the logic to the equals() method stub. Open the class Book and
add the logic to the equals() method. First we check if the
object given by the method is an instance of Book. Then
compare the properties title and price,
if they are equal return true.

public boolean equals(Object object) {<br> if (object instanceof Book) {<br>           Book book = (Book) object;<br>           return getTitle().equals(book.getTitle())<br>                   &amp;&amp; getPrice() == book.getPrice();<br>      }<br>      return false;<br>}


Create the suite() method


In order to run the test method testEquals()
add a method suite() to the class BookTest.
Note: You can also create a separate class
where you add the suite() method.
Within the method create a new instance of
TestSuite and use the method addTest(..) to add a test.
Here we use the dynamically way to add a test to a TestSuite.
The method looks like the follows:
public static Test suite(){<br>      TestSuite suite = new TestSuite();<br>      suite.addTest(new BookTest("testEquals"));<br> return suite;<br>}



Run the test


After finishing all test methods we want to
run the JUnit test case. Right mouse button on the class BookTest and
choose Run As > JUnit Test.
On the JUnit view (Menu Windows -> show
view) of Eclipse you can see how many runs, errors and failures
occurred.






No comments:

Post a Comment