EasyMock - Spring 采用原始对象而不是模拟对象
EasyMock - Spring taking the original object not the mocked object
在使用 EasyMock 测试我的 Spring classes 时,我遇到了以下情况:
我的 Spring 配置采用 Spring component-scan
配置的原始 DAO 对象,而不是我的模拟 DAO 对象。
请在下面找到我的模拟、AppContext 和测试 class:
ApplicationContxt-Test.xml
<context:annotation-config />
<context:component-scan base-package="com.test.testclasses"/>
<import resource="mockServices.xml" />
MockServices.xml
<bean class="org.easymock.EasyMock" factory-method="createMock"
id="codeDAO" primary="true" >
<constructor-arg value="com.test.testclasses.dao.MaintainCodeDAO" />
</bean>
JUnit class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContxt-Test.xml")
public class MaintainCodeServiceImplTest {
@Autowired
private MaintainCodeDAO codeDAO;
@Autowired
private MaintainCodeService maintainCodeService;
@Before
public void setUp() {
EasyMock.reset(codeDAO);
}
@After
public void tearDown() {
EasyMock.reset(codeDAO);
}
@Test
public void shouldAutowireDependencies() {
assertNotNull(codeDAO);
assertNotNull(maintainCodeService);
}
@Test
public void getProcedureByCode_success() throws Exception {
MaintainCodeVO maintainCodeVO = new MaintainCodeVO();
EasyMock.expect(codeDAO.searchProcedureCode(isA(String.class))).andReturn(maintainCodeVO);
EasyMock.replay(codeDAO);
MaintainCodeBO maintainCodeResult = maintainCodeService.getProcedureByCode("test");
EasyMock.verify(codeDAO);
codeDAO.searchProcedureCode("test");
assertNotNull(maintainCodeResult);
EasyMock.reset(codeDAO);
}
}
我在这里模拟 codeDAO
并且在测试服务 class 时,而不是模拟 codeDAO
,原始 DAO 对象正在自动装配并且 EasyMock.verify()
正在抛出例外。不知道是什么问题。上面的配置有没有问题?
如果我没记错的话,因为 createMock
是一个泛型方法,Spring 在推断 bean 类型时不能正常工作,它将它推断为 Object
,所以它不能接线。我建议不要在单元测试中使用 Spring,使用简单的 类.
顺便说一句,EasyMock 有它自己的 DI,直接使用它:http://easymock.org/user-guide.html#mocking-annotations
可以将模拟放入上下文中,例如如此处所述:Autowiring of beans generated by EasyMock factory-method? 但对我来说这不是最好的方法,看起来很笨拙。
如果您使用的是 Spring Framework 4.2 或更高版本,这应该可行。详情见以下相关问题
- https://jira.spring.io/browse/SPR-9567
- https://jira.spring.io/browse/SPR-9682
- Is there any way to override a bean discovered by component scan?
我找到了解决方案。诀窍是如此简单..我杀了一天找到解决方案..
所以这是解决方案:
只需将 mockServices.xml(spring xml 仅使用模拟配置)移动到 context:component-扫描...
上方
<import resource="mockServices.xml" />
<context:annotation-config />
<context:component-scan base-package="com.wellpoint.icnotes"/>
工作得很好。
在使用 EasyMock 测试我的 Spring classes 时,我遇到了以下情况:
我的 Spring 配置采用 Spring component-scan
配置的原始 DAO 对象,而不是我的模拟 DAO 对象。
请在下面找到我的模拟、AppContext 和测试 class:
ApplicationContxt-Test.xml
<context:annotation-config />
<context:component-scan base-package="com.test.testclasses"/>
<import resource="mockServices.xml" />
MockServices.xml
<bean class="org.easymock.EasyMock" factory-method="createMock"
id="codeDAO" primary="true" >
<constructor-arg value="com.test.testclasses.dao.MaintainCodeDAO" />
</bean>
JUnit class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContxt-Test.xml")
public class MaintainCodeServiceImplTest {
@Autowired
private MaintainCodeDAO codeDAO;
@Autowired
private MaintainCodeService maintainCodeService;
@Before
public void setUp() {
EasyMock.reset(codeDAO);
}
@After
public void tearDown() {
EasyMock.reset(codeDAO);
}
@Test
public void shouldAutowireDependencies() {
assertNotNull(codeDAO);
assertNotNull(maintainCodeService);
}
@Test
public void getProcedureByCode_success() throws Exception {
MaintainCodeVO maintainCodeVO = new MaintainCodeVO();
EasyMock.expect(codeDAO.searchProcedureCode(isA(String.class))).andReturn(maintainCodeVO);
EasyMock.replay(codeDAO);
MaintainCodeBO maintainCodeResult = maintainCodeService.getProcedureByCode("test");
EasyMock.verify(codeDAO);
codeDAO.searchProcedureCode("test");
assertNotNull(maintainCodeResult);
EasyMock.reset(codeDAO);
}
}
我在这里模拟 codeDAO
并且在测试服务 class 时,而不是模拟 codeDAO
,原始 DAO 对象正在自动装配并且 EasyMock.verify()
正在抛出例外。不知道是什么问题。上面的配置有没有问题?
如果我没记错的话,因为 createMock
是一个泛型方法,Spring 在推断 bean 类型时不能正常工作,它将它推断为 Object
,所以它不能接线。我建议不要在单元测试中使用 Spring,使用简单的 类.
顺便说一句,EasyMock 有它自己的 DI,直接使用它:http://easymock.org/user-guide.html#mocking-annotations
可以将模拟放入上下文中,例如如此处所述:Autowiring of beans generated by EasyMock factory-method? 但对我来说这不是最好的方法,看起来很笨拙。
如果您使用的是 Spring Framework 4.2 或更高版本,这应该可行。详情见以下相关问题
- https://jira.spring.io/browse/SPR-9567
- https://jira.spring.io/browse/SPR-9682
- Is there any way to override a bean discovered by component scan?
我找到了解决方案。诀窍是如此简单..我杀了一天找到解决方案.. 所以这是解决方案:
只需将 mockServices.xml(spring xml 仅使用模拟配置)移动到 context:component-扫描...
上方 <import resource="mockServices.xml" />
<context:annotation-config />
<context:component-scan base-package="com.wellpoint.icnotes"/>
工作得很好。