JUnit
How to Download and Install JUnit in Eclipse
Installing Junit is a 6 part process. It is explained in detailed below- PART 1) Install Java...
In Junit, test suite allows us to aggregate all test cases from multiple classes in one place and run it together.
To run the suite test, you need to annotate a class using below-mentioned annotations:
@Suite.SuiteClasses ({test1.class, test2.class……})
With above annotations, all the test classes in the suite will start executing one by one.
Step 1) Create a simple test class (e.g. MyFirstClassTest) and add a method annotated with @test.
Step 2) Create another test class to add (e.g. MySecondClassTest) and create a method annotated with @test.
Step 3) To create a testSuite you need to first annotate the class with @RunWith(Suite.class) and @SuiteClasses(class1.class2…..).
Step 4) Create a Test Runner class to run our test suite as given below;
Code Explanation:
Output: Here is the output which shows successful test with no failure trace as given below:
Consider a more complex example
JunitTest.java
JunitTest.java is a simple class annotated with @RunWith and @Suite annotations. You can list out number of .classes in the suite as parameters as given below:
package gtupapers.junit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
SuiteTest1.class,
SuiteTest2.class,
})
public class JunitTest {
// This class remains empty, it is used only as a holder for the above annotations
}
SuiteTest1.java
SuiteTest1.java is a test class having a test method to print out a message as given below. You will use this class as a suite in above mentioned class.
package gtupapers.junit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SuiteTest1 {
public String message = "Saurabh";
JUnitMessage junitMessage = new JUnitMessage(message);
@Test(expected = ArithmeticException.class)
public void testJUnitMessage() {
System.out.println("Junit Message is printing ");
junitMessage.printMessage();
}
@Test
public void testJUnitHiMessage() {
message = "Hi!" + message;
System.out.println("Junit Hi Message is printing ");
assertEquals(message, junitMessage.printHiMessage());
System.out.println("Suite Test 2 is successful " + message);
}
}
SuiteTest2.java
SuiteTest2.java is another test class similar to SuiteTest1.java having a test method to print out a message as given below. You will use this class as suite in JunitTest.java.
package gtupapers.junit;
import org.junit.Assert;
import org.junit.Test;
public class SuiteTest2 {
@Test
public void createAndSetName() {
String expected = "Y";
String actual = "Y";
Assert.assertEquals(expected, actual);
System.out.println("Suite Test 1 is successful " + actual);
}
}
Output
After executing JunitTest.java which contains a suite having test1.java and test2.java, you will get below output:
Summary
In this tutorial, you have learned basics of test harness and test suites in details with an example.
Installing Junit is a 6 part process. It is explained in detailed below- PART 1) Install Java...
What is Junit Assert? Assert is a method useful in determining Pass or Fail status of a test case,...
What is Junit? JUnit is an open source Unit Testing Framework for JAVA. It is useful for Java...
Sometimes you may require not to execute a method/code or Test Case because coding is not done...
What is Parameterized Test in Junit? Parameterized test is to execute the same test over and over...
What is JUnit Annotations? JUNIT ANNOTATIONS is a special form of syntactic meta-data that can be...