CodesJava

Easy learning with example program codes

Junit basic annotation example


Annotations for Junit testing:

1. @Test: It is used to specify the test method.

2. @BeforeClass: It is used to specify that method will be called only once, before starting all the test cases.

3. @AfterClass: It is used to specify that method will be called only once, after finishing all the test cases.

4. @Before: It is used to specify that method will be called before each test case.

5. @After: It is used to specify that method will be called after each test case.

6. @Ignore: It is used to ignore the test case.

Example:

DivisionTestCase.java

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
 
/**
 * This is test case class for basic JUnit annotation use.
 * @author codesjava
 */
public class DivisionTestCase {
	//called only once, before starting all the test cases.
	@BeforeClass
	public static void beforeClass() {
	   System.out.println("In beforeClass method.");
	}
 
	//called only once, after finishing all the test cases.
	@AfterClass
	public static void  afterClass() {
		System.out.println("In afterClass method");
	}
 
	//called before each test case.
	@Before
	public void before() {
	   System.out.println("In before method");
	}
 
	//called after each test case.
	@After
	public void after() {
	   System.out.println("In after method");
	}
 
	//define the test case.
	@Test
	public void testCase1() {
	   System.out.println("In testCase1");
	}
 
	//define the test case.
	@Test
	public void testCase2() {
	   System.out.println("In testCase2");
	}
 
	//ignore the test case.It will not execute.
	@Ignore
	@Test
	public void testCase3() {
	   System.out.println("In testCase3");
	}
 
}

Output:

In beforeClass method.
In before method
In testCase1
In after method
In before method
In testCase2
In after method
In afterClass method

 
junit1
 

Download this example.
 
Next Topic: JUnit expected exception test.
Previous Topic: JUnit framework.

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