Monday, September 15, 2014

JUnit Hello World example with Eclipse

This kick start guide will show you how to create and execute JUnit test cases with eclipse.

First create a new project in Eclipse. Then create a new class. I name the class as HelloWorld.java and create it inside the package com.example. I modify the HelloWorld class so that it looks like below.

package com.example;
public class HelloWorld {
  private String helloMessage;

  public HelloWorld(String message) {
    this.helloMessage = message;
  }

  public String sayHello() {
    return helloMessage;
  }
}

Right. Now what we need to do is create a JUnit test case to verify the functionality of this class.
Create another package in src folder and name it as com.tests(You may use any name).
Now right click on the newly created package and point to New ⇨ JUnit Test Case.

Set the name as HelloWorldTest. In "Class under test" text box browse and select the class HelloWorld which we created before. Click Next. Select the sayHello() method as below and click Finish. Note that test methods are generated only for the methods that you select here. If you want you can also select the HelloWord() also.



Now the generated test class will look like this.
package com.tests;

import static org.junit.Assert.*;
import org.junit.Test;

public class HelloWorldTest {
  @Test
  public void testSayHello() {
    fail("Not yet implemented");
  }
}

Now we can modify above auto generated method to add out  testing logics. I modify my HelloWorldTest class as below.

package com.tests;

import static org.junit.Assert.*;
import org.junit.Test;
import com.example.HelloWorld;

public class HelloWorldTest {
  String helloMessage = "Hello JUnit World!"; 
  HelloWorld hello = new HelloWorld(helloMessage);

  @Test
  public void testPrintMessage() {
     assertEquals(helloMessage, hello.sayHello());
  }
}

Ok, I have completed my test class now. Please take a look at testPrintMessage() method. What it does is, compares the String returned by the hello.sayHello() method with the String we set in constructor. As we know those should be the same. If those were different it means something has gone wrong.
assertEquals() method throws an error if the results were not the same.

Now right click the HelloWorldTest class and select Run As ⇨ JUnit Test. You will see that a tab like below will be opened and show the result of the test.



If we had multiple methods in our HelloWorldTest class there would be more lines in this report.If any of the methods failed those will be marked in red.
We can fail the test by modifying our class as below.

package com.tests;

import static org.junit.Assert.*;
import org.junit.Test;
import com.example.HelloWorld;

public class HelloWorldTest {
  String helloMessage = "Hello JUnit World!"; 
  HelloWorld hello = new HelloWorld(helloMessage + "abc");

  @Test
  public void testPrintMessage() {
     assertEquals(helloMessage, hello.sayHello());
  }
}

Now run the test again and the output will look like this.



The JUnit API class org.junit.Assert consists with lot more other important methods such as
     ● assertNotEquals();
     ● assertTrue();
     ● assertFalse();
     ● assertThat();

You can find the complete list in API documentation.
http://junit.sourceforge.net/javadoc/org/junit/Assert.html

No comments:

Post a Comment