This note documents the order in which the methods in the JUnit test class are run; And annotations commonly used in JUnit.

1.JUnit operation process

1.1 Creating a Test Class

Right-click the class under test and create a new test class. In the popup box, first change the code directory where the test class resides, and then check the four methods:

1.2 Modify the test class code

package com.fzhiy.junit02;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/ * * * *@author yufeng
 */
public class JunitFlowTest {

	/ * * *@throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass(a) throws Exception {
		System.out.println("this is setUpBeforeClass()...");
	}

	/ * * *@throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass(a) throws Exception {
		System.out.println("this is tearDownAfterClass()...");
	}

	/ * * *@throws java.lang.Exception
	 */
	@Before
	public void setUp(a) throws Exception {
		System.out.println("this is setUp()...");
	}

	/ * * *@throws java.lang.Exception
	 */
	@After
	public void tearDown(a) throws Exception {
		System.out.println("this is tearDown()...");
	}

	@Test
	public void test(a) {
		System.out.println("this is test()..."); }}Copy the code

Running results:



Add a test2() test method to the test class:

@Test
public void test2(a){
	System.out.println("this is test2()...");
}
Copy the code

Run it again and the result is as follows:

1.3 Summary

1) BeforeClass modified methods are executed before all methods are called, and the method is static, so it is run when the test class is loaded, and there is only one instance of it in memory, which is good for loading configuration files, 2) @AfterClass decorates methods that are executed after all methods have been called, usually to clean up resources, such as closing a connection to a database

3) @before and @after are executed Before and After each test method.

2.JUnit common annotations

@test: Modify a common method to a Test method @test (expected= xx.class) @test (timeout= milliseconds) @beforeclass: Static modifier @afterClass: it is executed After all methods have been run. Static modifier @before: it is executed once Before each test method has been run. @ignore: the modified test method is ignored by the test runner @runwith: You can change the test runner org.junit.runner.Runner

2.1 Tests for @test and @ignore

Example code:

package com.fzhiy.junit02;

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;

/ * * * *@author yufeng
 */
public class AnotationTest {

	@Test(expected=ArithmeticException.class)
	public void testDivide(a) {
		assertEquals("There's a problem with division.".3.new Calculate().divide(6.0)); // Set the divisor to 0
	}
	
	@Test(timeout=2000)
	public void testWhile(a) {
		while(true) {
			System.out.println("run forever...");  // An infinite loop}}@Test(timeout=3000)
	public void testReadFile(a){
		try {
			Thread.sleep(2000);       // Simulate a file read operation
		} catch(InterruptedException e) { e.printStackTrace(); }}@Ignore("..." )
	@Test
	public void testIgnore(a) {
		System.out.println("Will it work?"); }}Copy the code

Output result:





Description:

1) testDivide () method, the divisor is set to 0, would have thrown the Error, but set up @ Test (expected = ArithmeticException. Class), that we expect it will throw a arithmetic exception, so the program results also accord with our expectations.

② The testWhile() method is an infinite loop, but @test (timeout=2000) is set to automatically end the loop after 2 seconds

③ The testReadFile() method simulates the file reading operation, and sets the read timeout time to 3 seconds. If the test time is equal to or greater than the test time, the program is considered unsuccessful, and the program sleeps for 2 seconds without timeout. It is shown here as some performance test.

The testIgnore() method will not run because of the @ignore annotation.

2.2 @ RunWith

When a class is modified by the @runwith annotation, or when a class inherits a class modified by the annotation, JUnit will use the runner specified by the annotation to run the test, rather than JUnit’s default runner.