CodesJava

Easy learning with example program codes

Junit ignore test


The @Ignore annotation is used to ignore test cases. The @Ignore annotation can be used with method or class. If it is used with the method then that method will be ignored by JUnit and will not execute and if it is used with the class then all methods of that class will be ignored and no method will execute.

Junit ignore test example at method level:

DivisionTestCase.java

import com.javawithease.business.*;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
 
/**
 * This is test case class for ignore test at method level.
 * @author codesjava
 */
public class DivisionTestCase {
	//DivisionTest class objects
	DivisionTest divisionTest1 = new DivisionTest(10, 2); 
	DivisionTest divisionTest2 = new DivisionTest(10, 0); 
 
	//Test case for division, As we specify ignore annotation,
	//this test case will not execute.
	@Test
	@Ignore
	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 codesjava
 */
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:

junit3

 

Download this example.

Junit ignore test example at class level:

DivisionTestCase.java

import com.javawithease.business.*;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
 
/**
 * This is test case class for ignore test at class level.
 * As we specify ignore annotation at class 
 * no test case will execute.
 * @author codesjava
 */
@Ignore
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());
	}
}

DivisionTestCase.java

/**
 * This is simple java class containing division method.
 * @author codesjava
 */
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:

junit4

 
Download this example.
 
Next Topic: JUnit time test.
Previous Topic: JUnit expected exception 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.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki