• September 4, 2014
  • JUnit
  • Comments Off on Junit test runner

Junit test runner

Test runner is used for executing the test cases.

Example:

DivisionTestCase.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 DivisionTestCase {
	//DivisionTest class objects
	DivisionTest divisionTest1 = new DivisionTest(10, 2); 
	DivisionTest divisionTest2 = new DivisionTest(10, 0); 
 
	//Test case for division
	@Test
	public void 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() {
		assertEquals(5, divisionTest2.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;
	}
}

TestRunner.java

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
 
/**
 * This is a test runner class.
 * @author javawithease
 */
public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(DivisionTestCase.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}

Output:

true

Download this example.
 
Previous Topic: JUnit parameterized test.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.