概述
Jmock和Mockito是常用的mock工具,Jmock功能相当强大,也比较容易用。Mockito 在处理多层嵌套时比较麻烦,下面说明了这种场景的主要用法。不过建议不是用jmock。
package com.jd.test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.when;
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.jd.test.service.CustomerBusiness;
import com.jd.test.service.FeeBusiness;
import com.jd.test.service.OrderBusiness;
import com.jd.test.service.ProductBusiness;
import com.jd.test.service.impl.FeeBusinessImpl;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring-config.xml")
public class MockitoTest {
@Resource
@InjectMocks
private ProductBusiness injectProductBusiness;
@Mock
private OrderBusiness orderBusiness;
@Resource
@InjectMocks
private CustomerBusiness customerBusiness;
@Spy
private FeeBusiness feeBusiness = new FeeBusinessImpl();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
/**
* 自动注入mock管理的bean. 解决二层和多层bean嵌套的部分mock场景。即injectProductBusiness->customerBusiness->feeBusiness 时,如果想mock feeBusiness中的某个方法.
*
* @Resource
@InjectMocks
private ProductBusiness injectProductBusiness;
@Resource
@InjectMocks
private CustomerBusiness customerBusiness;
@Spy
private FeeBusiness feeBusiness = new FeeBusinessImpl();
*
* @InjectMocks 标签会自动填充带@Spy和@Mock标签的bean.
* @Spy 被它代理的bean,默认执行原生方法。但可以在后期mock想要的方法。
* @Mock 相当于mockito帮助简单的实例化bean,因此无法执行原生方法。适用于整个bean都mock,如DAO。
*
* 同时可以结合spring一起管理bean.对bean的管理应该是spring先进行一系列的如初始化bean操作,然后mockito会引用spring生成的bean,并对bean里的指定field进行重新注入。以达到实现部分mock功能 。
*/
@Test
public void injectClass() {
when(feeBusiness.getFee(anyLong())).thenReturn("spy inject");
assertEquals(injectProductBusiness.getCustomerName(1), "productBusinessimpl->null");
assertEquals(injectProductBusiness.getCustomerPhone(2),"productBusinessimpl->CustomerBusinessImpl->2");
assertEquals(injectProductBusiness.getOrderFee(3),"productBusinessimpl->CustomerBusinessImpl->spy inject");
}
}
相关类:
package com.jd.test.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.jd.test.domain.Order;
import com.jd.test.service.OrderBusiness;
@Component
public class OrderBusinessImpl implements OrderBusiness {
public Order getOrder(long orderid) {
Order order = new Order();
order.setAddress("北京市");
order.setOrderid(orderid);
return order;
}
public String getOrderAddress(long orderid) {
return "address:" + orderid;
}
public String getCustomerName(long orderid) {
return "name:" + orderid;
}
}
package com.jd.test.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.jd.test.service.CustomerBusiness;
import com.jd.test.service.FeeBusiness;
@Component
public class CustomerBusinessImpl implements CustomerBusiness{
@Resource
private FeeBusiness feeBusiness;
public String getCustomerPhone(long orderid) {
return "CustomerBusinessImpl->" + orderid;
}
public String getOrderFee(long id){
return "CustomerBusinessImpl->" + feeBusiness.getFee(id);
}
}
package com.jd.test.service.impl;
import org.springframework.stereotype.Component;
import com.jd.test.service.FeeBusiness;
@Component
public class FeeBusinessImpl implements FeeBusiness{
public String getFee(long id) {
return "FeeBusinessImpl-->" + id;
}
public String getAmount(long id) {
return "FeeBusinessImpl-->" + id;
}
}
最后
以上就是喜悦雪碧为你收集整理的Mockito bean多层嵌套的部分mock使用的全部内容,希望文章能够帮你解决Mockito bean多层嵌套的部分mock使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复