JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写。
方法注解:
@After:
使用了该元数据的方法在每个测试方法执行之后要执行一次。
@Test
用了该元数据的方法是测试方法。没有参数和返回值。
限时测试(timeout = 1000)
异常测试(expected = ArithmeticException.class)
@RunWith
打包测试:
Mokito:
方法注解:
@BeforeClass:
使用了该元数据的方法在所有测试方法执行前只执行一次。
@AfterClass:
使用了该元数据的方法在所有测试方法执行后只执行一次。
@Before:
使用了该元数据的方法在每个测试方法执行之前都要执行一次。@After:
使用了该元数据的方法在每个测试方法执行之后要执行一次。
@Test
用了该元数据的方法是测试方法。没有参数和返回值。
限时测试(timeout = 1000)
异常测试(expected = ArithmeticException.class)
@ignore:
该元数据标记的测试方法在测试中会被忽略。
@RunWith
参数化测试
- 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},
- });
- }
- //构造函数,对变量进行初始化
- public SquareTest(int param, int result){
- this.param = param;
- this.result = result;
- }
- @Test
- public void square(){
- calculator.square(param);
- assertEquals(result, calculator.getResult());
- }
- }
- import org.junit.runner.RunWith;
- import org.junit.runners.Suite;
- @RunWith(Suite.class)
- @Suite.SuiteClasses(...{CalculatorTest.class, SquareTest.class})
- public class AllCalculatorTests{}
性能测试:
- 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 {
- }
- }
http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html