JUnit time test

The time test is used to check that if a test case is completed within the specified time or not. JUnit terminate and mark it failed automatically if it takes more time than the specified milliseconds. We have to specify time in milliseconds in @Test(timeout = timeInMilliSeconds) action.


import com.javawithease.business.*;
import static org.junit.Assert.*;
import org.junit.Test;
* This is test case class for time out test.
* @author javawithease

public class DivisionTestCase {
//DivisionTest class objects
DivisionTest divisionTest1 = new DivisionTest(10, 2);
DivisionTest divisionTest2 = new DivisionTest(10, 0);
//Test case for division
public void test() {
assertEquals(5, divisionTest1.division());
//Test case for print result.
public void testPrintResult() {
//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() {
assertEquals(5, divisionTest2.division());
* This is simple java class containing division method.
* @author javawithease

public class DivisionTest {
//data members
int num1, num2, result = 0;
//parameterised constructor
public DivisionTest(int num1, int num2){
this.num1 = num1;
this.num2 = num2;
//division method
public int division() throws ArithmeticException{
result = num1/num2;
return result;
//print result, while(true) is used for infinite loop.
public void printResult(){
System.out.println("Result = " +result);



No comments: