Tuesday, 30 September 2014

Working with JUnit in MB Toolkit using Eclipse


Step 1: Download Junit JAR file named "junit4/junit4-4.8.2.jar.zip" from this location

http://www.java2s.com/Code/Jar/j/Downloadjunit4482jar.htm

Step 2: Set Eclipse environment

  • Add the JAR file in the References section
Add junit-4.10.jar in liraries.

Now your eclipse is ready for the development of JUnit test cases.

Step 3: Verify Junit installation in Eclipse

  • Create a project TestJunit in eclipse at any location.
  • Create a class MessageUtil to test in the project
   
/*
* 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;
   }   
} 
  • Create a test class TestJunit in the project
   
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class TestJunit {
 
   String message = "Hello World"; 
   MessageUtil messageUtil = new MessageUtil(message);

   @Test
   public void testPrintMessage() {   
      assertEquals(message,messageUtil.printMessage());
   }
}
Following should be the project structure
Project Structure
Finally, verify the output of the program by right click on program and run as junit
Run Junit
Verify the result
Junit result success.