Unit Test 单元测试

JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写。

方法注解:

@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 {
	
	}
}


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值