Unit Test Unit Test

JUnit4 is the biggest improvement in the history of the JUnit framework, and its main goal is to use the Annotation feature of Java5 to simplify the writing of test cases.

Method annotation:

@BeforeClass:
A method using this metadata is executed only once before all test methods are executed.
@AfterClass:
A method using this metadata is executed only once after all test methods are executed.

@Before:

Methods that use this metadata are executed once before each test method.
@After:
Methods that use this metadata are executed once after each test method is executed.
@Test
methods that use this metadata are test methods. There are no parameters and no return value.
Time-limited test (timeout = 1000)
Exception test (expected = ArithmeticException.class)

@ignore:
Test methods marked with this metadata will be ignored during testing.

 

Class annotation:
@RunWith

Parametric test

 

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class SquareTest ...{
	private static Calculator calculator = new Calculator();
	private int param;
	private int result;    

	@Parameters   
	public static Collection data() ...{
	        return Arrays.asList(new Object[][]{
                {2, 4},
                {0, 0},
                {-3, 9},
        });
}

	//Constructor to initialize variables
	public SquareTest(int param, int result){
		this.param = param;
        	this.result = result;

	}

	@Test   
	public void square(){
	        calculator.square(param);
	        assertEquals(result, calculator.getResult());
	}

}


Package test:

 

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses(...{CalculatorTest.class, SquareTest.class})
public class AllCalculatorTests{}

 

Performance Testing:

 

public class Test {
	@Rule
	public ContiPerfRule i = new ContiPerfRule ();     
 	
	@Test
	@PerfTest(invocations = 100, threads = 2)
	@Required(average = 25, percentile95 = 1000)
	public void test() throws Exception {
	
	}
}


Mokito: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326640494&siteId=291194637