JUnit - Plug with Eclipse
To setup JUnit with eclipse following steps need to be followed
Step 1: Download Junit archive
Download JUnit
| OS | Archive name |
|---|---|
| Windows | junit4.10.jar |
| Linux | junit4.10.jar |
| Mac | junit4.10.jar |
Assume you copied above JAR file in C:\>JUnit folder.
Step 2: Set Eclipse environment
Open eclipse -> right click on project and click on property > Build Path > Configure Build Path and add the junit-4.10.jar in the libraries using Add External Jar button.
We assume that your eclipse has inbuilt junit plugin if it is not available in C:\>eclipse/plugins directory, then you can download it from JUnit Plugin. Unzip the downloaded zip file in the plugin folder of the eclipse. Finally restart eclipse.
Now your eclipse is ready for the development of JUnit test cases.
Step 3: Verify Junit installation in Eclipse
Create a projet 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
Finally, verify the output of the program by right click on program and run as junit
Verify the result