Spring中如何使用Mockito做单元测试

来源:互联网 发布:返利cms 编辑:程序博客网 时间:2024/05/16 19:24

在Spring环境下做单元测试常常遇到很多问题,Controller层还好说,Spring本身提供了很多方法来支持,这个可以看我的另一篇文章

http://blog.csdn.net/ynwso/article/details/8672310


但是在服务层和持久层,如果是使用注解注入的方式来设计的话,用Mock来替换真实的注入类就很麻烦了,还好我们有Mockito框架。但一段代码,你会发现它比easyMock好用多了

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath*:application-context-test.xml" })public class RouteServiceTestCase{   @InjectMocks   @Autowired   private IRouteService service;   @Mock   private IRouteMatrixDataProvider provider;   @Before   public void myBefore() {       MockitoAnnotations.initMocks(this);   }   @Test   public void testGetAirlineCode() {       RouteMatrix rm = new RouteMatrix();       rm.setAirlineCode("kkk");       Mockito.when(this.provider.getRevenueRoute("HKG", "AMM", true)).thenReturn(rm);       String code = this.service.getAirlineCode("HKG", "AMM", this.brand, this.cInfo, true);       Assert.assertNotNull(code);       Assert.assertEquals("kkk", code);       code = this.service.getAirlineCode("HKG", "KKK", this.brand, this.cInfo, true);       Assert.assertNull(code);   }   @Test   public void testGetAirlineCode2() {       String code = this.service.getAirlineCode("HKG", "AMM", this.brand, this.cInfo, true);       Assert.assertNotNull(code);       Assert.assertEquals("kkk", code);   }}

@Spy
@Autowired
private IRouteMatrixDataProvider provider;


service 被标记了 @InjectMocks , 在 myBefore方法中 执行 MockitoAnnotations.initMocks(this); 的时候,会将标记了 @Mock 或 @Spy 的属性注入到 service 中。
如果是 @Mock, 那就是通常的方式,service里面的provider完全被Mock实例替换,所有的调用都是针对Mock生成类的。
如果是 @Autowired 加 @Spy , 那么对于定制了返回值的会调用 Mock实例,否则会调用真实注入的属性,但是这里有一个限制,如果是代理类会报错,比如Spring 的AOP代理。 
对于AOP的代理类,如果想一部分用Mock,一部分用真实的实例,感觉目前是有些困难,暂时想到的办法就是写两个测试类,一个注入真实属性,一个注入mock属性。


方法调用时,如果不想指定一个明确的参数,就可以用下面这样的写法来表示任意的参数。

Mockito.when(this.provider.getRevenueRoute(Matchers.anyString(), Matchers.anyString(), Matchers.anyBoolean()))           .thenReturn(rm);

但是这里也有一个限制就是,如果有一个参数使用了any***(),则所有的参数都必需使用这种方式,不能像下面这样写

Mockito.when(this.provider.getRevenueRoute(Matchers.anyString(), Matchers.anyString(), true))


原创粉丝点击