【发布时间】:2014-10-21 06:21:21
【问题描述】:
我想模拟一个类似这样的 DAO 管理器
public class DaoManager{
@Autowired
private Service1 service;
@Autowired
private Service2 service 2;
@Autowired
private DAOManager1 manager 1;
public CustomerDetail getCustomerDetails(){
manager1.getCustomerDetails();
}
public class DaoManager1{
@Autowired
private Service3 service3;
@Autowired
private Service4 service 4;
public getCustomerDetails(){
Service3.getCustName();
Service4.getCustAddress();
}
我的问题是如何模拟 DaoManager 类? 如果我模拟它,我需要模拟从 getCustomerDetails 方法调用的每一个经理/服务? 对我来说,这似乎是一个很大的开销。有什么想法或我完全弄错了吗?
编辑:
当我运行 junit 时,出现以下错误。
原因: org.springframework.beans.factory.BeanCreationException:创建名为“mockDaoManager”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 x.y.z.Service2 x.y.z.Service2;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [x.y.z.Service2] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
【问题讨论】:
标签: java unit-testing mocking mockito junit4