Monday, 17 February 2014

Create and Run JUnit Test Cases Using Eclipse

Hi All,

In this post I am going to discuss about how to write and run JUnit test cases using eclipse IDE. Eclipse has built in support for creating JUnit test cases.


Before going to know how to create and run JUnit test cases, lets see some most frequently used methods, annotations, terminologies in JUnit



Frequently used methods, annotations, terminologies :



setUp() / @Before :
Both setUp() and @Before serves the same purpose. Where as setUp() method is used in JUnit 3 and @Before is used in JUnit 4.
This method executes before running the JUnit test case. Any logic which needs to be executed before running the test case, can be written in this method.

The method annotated with @Before annotation need not be necessarily have the name setUp() it can have other name also. 


tearDown() / @After :
Both tearDown() and @After servers the same purpose. Where as tearDown() method is used in JUnit 3 and @After is used in JUnit 4.

This method executes after running the JUnit test case. Any logic which needs to be executed after running the test case, can be written in this method.

The method annotated with @After annotation need not be necessarily have the name tearDown() it can have other names also.


@Test :
In JUnit 4 every method which in which we are writing test cases should be annotated with @Test annotation, otherwise it will not be treated as a test case.


@Ignore :
If you want to ignore the execution of a JUnit test case, simply you can annotate with @Ignore annotation.


Fixture :
Fixture is the object of the class which is under test



Steps to create a JUnit test case :

  • First create a new java project using new java project menu item. Give it the name you want, I am giving it as junit.
  • Create a package with name com.raju.example in src 
  • Create a package with name com.raju.example.test in src, this is the package in which we are going to create JUnit test.
  • Create a class with name User in com.raju.example package
      /**
       * 
       */
       package com.raju.example;


       public class User {

private Integer id;

private String userName;

private String firstName;

private String lastName;

private Float salary;


// Method to which we are going to write test case

public User getDetails() {

User u = new User();
u.setFirstName("Raju");
u.setLastName("Rudru");
u.setUserName("rajurudru");
u.setSalary(10000f);

return u;
}

// getter and setter methods
    }

    • The package structure looks as shown below



                                                                                                                                                                                                                                                       

                                                                                                                                                                                                                                                • Right click on package com.raju.example.test and go New->Other



                                                                                                                                                                                                                                                • In the New wizard opened, type Junit in the filter text and select JUnit Test Case and click Next

                                                                                                                                                                                                                                                • In the next window, you can choose the version of the JUnit you want to work with i.e. JUnit 3 (no annotations support) or JUnit 4 (annotations support). I have chosen JUnit 4
                                                                                                                                                                                                                                                • Type name of class as UserTest, you can Browse class under test and choose User class, for which I want to write test cases.
                                                                                                                                                                                                                                                • If you select JUnit 4 and select setUp() method it will generate @Before annotation on top of the setup method, and @After for tearDown() method
                                                                                                                                                                                                                                                • You can choose which methods to be created in the test class, for example setUp() and tearDown().
                                                                                                                                                                                                                                                • Click on Next button


                                                                                                                                                                                                                                                • In the next window you can select the methods for which you want to create test cases, click on Finish



                                                                                                                                                                                                                                                • Since JUnit 4 library is not added to the project, it will prompt you to Add JUnit 4 libraray to the build path. Click on Ok



                                                                                                                                                                                                                                                • Now eclipse will create UserTest class as shown below. Eclipse creates a bare template of the test class, it is developer's responsibility to implement the logic in test cases.

                                                                                                                                                                                                                                                                  import static org.junit.Assert.*;

                                                                                                                                                                                                                                                                  import org.junit.After;
                                                                                                                                                                                                                                                                  import org.junit.Before;
                                                                                                                                                                                                                                                                  import org.junit.Test;


                                                                                                                                                                                                                                                                  public class UserTest {

                                                                                                                                                                                                                                                        /**
                                                                                                                                                                                                                                                                 * @throws java.lang.Exception
                                                                                                                                                                                                                                                         */
                                                                                                                                                                                                                                                         @Before
                                                                                                                                                                                                                                                                public void setUp() throws Exception {
                                                                                                                                                                                                                                                          }

                                                                                                                                                                                                                                                               /**
                                                                                                                                                                                                                                                               * @throws java.lang.Exception
                                                                                                                                                                                                                                                                */
                                                                                                                                                                                                                                                               @After
                                                                                                                                                                                                                                                               public void tearDown() throws Exception {
                                                                                                                                                                                                                                                               }

                                                                                                                                                                                                                                                               /**
                                                                                                                                                                                                                                                               * Test method for {@link com.raju.example.User#getDetails()}.
                                                                                                                                                                                                                                                                */
                                                                                                                                                                                                                                                               @Test
                                                                                                                                                                                                                                                               public void testGetDetails() {
                                                                                                                                                                                                                                                               fail("Not yet implemented");
                                                                                                                                                                                                                                                               }

                                                                                                                                                                                                                                                               }

                                                                                                                                                                                                                                                NOTE:

                                                                                                                                                                                                                                                By default eclipse will generate test method with body as fail("Not yet implemented"); if you run the method your test case will be failed. You need to implement logic inside the test method.


                                                                                                                                                                                                                                                Lets change the code in UserTest class to write test case for getDetails() method in User class:




                                                                                                                                                                                                                                                • Create a class level variable for User class in UserTest class as



                                                                                                                                                                                                                                                private User fixture;   
                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                    This is called as fixture (Object of class under test)

                                                                                                                                                                                                                                                       

                                                                                                                                                                                                                                                • Create object for User in setUp() method. setUp() method will look as shown below


                                                                                                                                                                                                                                                @Before                       
                                                                                                                                                                                                                                                public void setUp() throws Exception {
                                                                                                                                                                                                                                                fixture = new User();                
                                                                                                                                                                                                                                                }      
                                                                                                                                                                                                                                                                               


                                                                                                                                                                                                                                                • Now you can write the test case logic in testGetDetails() method.



                                                                                                                                                                                                                                                        @Test                                        
                                                                                                                                                                                                                                                public void testGetDetails() {                       
                                                                                                                                                                                                                                                assertTrue(fixture.getDetails() instanceof User);
                                                                                                                                                                                                                                                       }                                    

                                                                                                                                                                                                                                                 Above method tests whether the returned object from getDetails() method in User class is of type User or not. If it is true the test case  passes otherwise test case fails.

                                                                                                                                                                                                                                                assertTrue() is a static method in org.junit.Assert package. We use static methods in org.junit.Assert package to make comparisions of the expected results in test methods.

                                                                                                                                                                                                                                                The modified UserTest class will look as given below


                                                                                                                                                                                                                                                /**
                                                                                                                                                                                                                                                 * 
                                                                                                                                                                                                                                                 */
                                                                                                                                                                                                                                                package com.raju.example.test;

                                                                                                                                                                                                                                                import static org.junit.Assert.*;

                                                                                                                                                                                                                                                import org.junit.After;
                                                                                                                                                                                                                                                import org.junit.Before;
                                                                                                                                                                                                                                                import org.junit.Test;

                                                                                                                                                                                                                                                import com.raju.example.User;


                                                                                                                                                                                                                                                public class UserTest {

                                                                                                                                                                                                                                                private User fixture;

                                                                                                                                                                                                                                                /**
                                                                                                                                                                                                                                                * @throws java.lang.Exception
                                                                                                                                                                                                                                                */
                                                                                                                                                                                                                                                @Before
                                                                                                                                                                                                                                                public void setUp() throws Exception {
                                                                                                                                                                                                                                                fixture = new User();
                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                /**
                                                                                                                                                                                                                                                * @throws java.lang.Exception
                                                                                                                                                                                                                                                */
                                                                                                                                                                                                                                                @After
                                                                                                                                                                                                                                                public void tearDown() throws Exception {
                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                /**
                                                                                                                                                                                                                                                * Test method for {@link                                            com.raju.example.User#getDetails()  
                                                                                                                                                                                                                                                         */
                                                                                                                                                                                                                                                @Test
                                                                                                                                                                                                                                                public void testGetDetails() {


                                                                                                                                                                                                                                                assertTrue(fixture.getDetails() instanceof User);

                                                                                                                                                                                                                                                }


                                                                                                                                                                                                                                                }



                                                                                                                                                                                                                                                Running the JUnit test:


                                                                                                                                                                                                                                                If you want to run all the test cases at one shot, you can right click in the empty area of a class and got to Run As -> JUnit test, it will run all the test methods annotated with @Test

                                                                                                                                                                                                                                                For running a single JUnit test case you can simply select the method name and right click -> Run As -> JUnit Test




                                                                                                                                                                                                                                                You can see the test result in JUnit console



                                                                                                                                                                                                                                                As shown in the above figure, green color represents the success state of test case, red color represents the failure state. You can see the Failure Trace under Failure Trace tab.