JUnit与JMock学习

时间:2021-11-07 15:55:14

测试驱动编程和持续集成部署应该说是现在软件开发者的必备武器,不过跟其他很多好东西一样,在我们公司的推广总要慢上一拍,毕竟老板看的是你能够把功能实现好让客户满意,所以能不折腾就不折腾。但不管用不用,先学下单元测试技术吧。

JUnit单元测试

用JUnit写单元测试,首先是定义一个单元测试类;然后在类中定义测试方法,使用@Test标签即可。


重要标签

ANNOTATION DESCRIPTION
@Test 定义test case
@Test(expected = Exception.class) 如果没有抛出某个异常则失败   
@Test(timeout=100) 如果超过多少毫秒则失败
@Before 在每个test case前执行
@After 在每个test case后执行
@BeforeClass 在每个test class前执行
@AfterClass 在每个test class执行
@Ignore 忽略执行某个测试方法

运行多个测试集

可以定义一个测试集来依次运行多个测试类

 package com.vogella.junit.first;

 import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class)
@SuiteClasses({ MyClassTest.class, MySecondClassTest.class })
public class AllTests { }

JMockit使用

单元测试一般只测试某一个功能,但是由于类之间的耦合性往往难以把功能隔离开来。例如你希望测试某个业务逻辑处理数据的功能,但是数据是从Database取回的,这就涉及到DAO层的类调用;你不希望单元测试函数去访问数据库(除非是测试DAO的单元测试),于是你希望有一个假的DAO类刚好返回你需要的测试数据。Mock的作用就是在单元测试里模拟类的行为和状态。市面上有好几种Mock库,包括EasyMock, Mockit等,这里我还是推荐功能更加强大的JMockit库。

JMockit有好几种不同的使用方式,但基本的思路相同。第一就是需要用到哪些Mock类或者方法,就定义多少,绝不Mock无用的类和方法。第二在编写程序时候就需要尽量使得类之间的调用接口化。

第一种是所谓的"State-oriented mocking",也就是基于状态的模拟,有个例子:

 package com.test;

 import mockit.MockUp;

 import org.junit.Test;
import junit.framework.TestCase; /**
* 类名:
* 类说明:
*
* @version V1.0
* @author lu7kang 2011-3-31
*/
public class MyTestCase extends TestCase { @Test
public void test() throws Exception{
// mock第三方接口方法-返回成功
MockUp mu3 = new MockUp() {
@Mock
public EFaxResult updateNumberProfile(NumberProfile numberProfile, String memberId) {
EFaxResult rst = new EFaxResult();
// mock第三方方法-返回成功100
rst.setResultCode(ResultCode.RC_100);
return rst;
}
};
// 测试本地代码
MyService myservice = new MyServiceImpl();
NumberProfile numberProfile = new NumberProfile();
String memberId = "test_id";
// 测试自己的方法
rst = myservice.doMyNumberProfile(numberProfile, memberId);
Assert.assertEquals(ResultCode.RC_100, rst.getResultCode());
}
}

在MyService的实现里用到了ESPService的updateNumberProfile方法,于是测试中通过创建一个new MockUp()类(并实现updateNumberProfile方法),可以让JMockit在单元测试运行过程中用Mock的updateNumberProfile代替原生的调用。

还有一种Behavior Based使用方法,其定义mock的方式就是先录制好某些方法调用和返回结果。例如:

 @Mocked T mock1; // all current and future instances are mocked
@Injectable K anotherMock; // only one particular instance is mocked @Test
public void aTestMethod()
{
new NonStrictExpectations() {{
mock1.getData(); result = "my test data";
anotherMock.doSomething(anyInt, "some expected value", anyString); times = 1;
}}; // In the replay phase, the tested method would call the "getData" and "doSomething"
// methods on a "MyCollaborator" instance.
... // In the verify phase, we may optionally verify expected invocations to "MyCollaborator"
// objects.
...
}

参考链接

Junit教程
Mockito教程
JMockit教程

本条目发布于 2014 年 03 月 11 日。属于Java 分类。