JUnit suite test

The suite test refers to group a few unit test cases and run it together. The @RunWith and @Suite annotation are used to run the suite test.

Example:

TestSuite.java
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
 
/**
* This class is used as suite test class.
* Both DivisionTestCase1 and DivisionTestCase2
* executes together after TestSuite is executed.
* @author javawithease
*/

 
@RunWith(Suite.class)
@Suite.SuiteClasses({
DivisionTestCase1.class,
DivisionTestCase2.class
})
public class TestSuite {
 
}
DivisionTestCase1.java
import com.javawithease.business.*;
import static org.junit.Assert.*;
import org.junit.Test;
 
/**
* This is test case class for division method.
* @author javawithease
*/

public class DivisionTestCase1 {
//DivisionTest class objects
DivisionTest divisionTest1 = new DivisionTest(10, 2);
DivisionTest divisionTest2 = new DivisionTest(10, 0);
 
//Test case for division
@Test
public void test() {
System.out.println("In DivisionTestCase1.test");
assertEquals(5, divisionTest1.division());
}
 
//Test case for expected ArithmeticException,
//As in this case ArithmeticException
// is the expected exception so JUnit
//will pass this unit test.
@Test(expected = ArithmeticException.class)
public void testException() {
System.out.println("In DivisionTestCase1.testException");
assertEquals(5, divisionTest2.division());
}
}
DivisionTestCase2.java
import com.javawithease.business.*;
import static org.junit.Assert.*;
import org.junit.Test;
 
/**
* This is test case class for division method.
* @author javawithease
*/

public class DivisionTestCase2 {
//DivisionTest class objects
DivisionTest divisionTest = new DivisionTest(10, 5);
 
//Test case for division
@Test
public void test() {
System.out.println("In DivisionTestCase2.test");
assertEquals(2, divisionTest.division());
}
}
DivisionTest.java
/**
* This is simple java class containing division method.
* @author javawithease
*/

public class DivisionTest {
//data members
int num1, num2;
 
//parameterised constructor
public DivisionTest(int num1, int num2){
this.num1 = num1;
this.num2 = num2;
}
 
//division method
public int division() throws ArithmeticException{
return num1/num2;
}
}

Output:

In DivisionTestCase1.test
In DivisionTestCase1.testException
In DivisionTestCase2.test

No comments: