JUnit Suite Test

Advertisements


Test suite

Test suite means bundle a few unit test cases and run it together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test. This tutorial will show you an example having two TestJunit1 & TestJunit2 test classes to run together using Test Suite.

Create a Class

Create a java class to be tested say MessageUtil.java in C:\ > JUNIT_WORKSPACE

/*
* This class prints the given message on console.
*/
public class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   public MessageUtil(String message){
      this.message = message; 
   }

   // prints the message
   public String printMessage(){
      System.out.println(message);
      return message;
   }   

   // add "Hi!" to the message
   public String salutationMessage(){
      message = "Hi!" + message;
      System.out.println(message);
      return message;
   }   
}  	

Create Test Case Classes

Create a java class file name TestJunit1.java in C:\ > JUNIT_WORKSPACE

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestJunit1 {

   String message = "Robert";	
   MessageUtil messageUtil = new MessageUtil(message);
   
   @Test
   public void testPrintMessage() {	
      System.out.println("Inside testPrintMessage()");    
      assertEquals(message, messageUtil.printMessage());     
   }
}

Create a java class file name TestJunit2.java in C:\ > JUNIT_WORKSPACE

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestJunit2 {

   String message = "Robert";	
   MessageUtil messageUtil = new MessageUtil(message);
 
   @Test
   public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Robert";
      assertEquals(message,messageUtil.salutationMessage());
   }
}

Create Test Suite Class

  • Create a java class.

  • Attach @RunWith(Suite.class) Annotation with class.

  • Add reference to Junit test classes using @Suite.SuiteClasses annotation

Create a java class file name TestSuite.java in C:\ > JUNIT_WORKSPACE to execute Test case(s)

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
   TestJunit1.class,
   TestJunit2.class
})
public class JunitTestSuite {   
}  	

Create Test Runner Class

Create a java class file name TestRunner.java in C:\ > JUNIT_WORKSPACE to execute Test case(s)

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(JunitTestSuite.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}  	

Compile all java classes using javac

C:\JUNIT_WORKSPACE>javac MessageUtil.java TestJunit1.java 
TestJunit2.java JunitTestSuite.java TestRunner.java

Now run the Test Runner which will run test case defined in provided Test Case class.

C:\JUNIT_WORKSPACE>java TestRunner

Verify the output.

Inside testPrintMessage()
Robert
Inside testSalutationMessage()
Hi Robert
true


Advertisements
Advertisements