简体   繁体   English

春季:覆盖/模拟依赖项的依赖项

[英]Spring: Overriding/Mocking dependency's dependency

Running test cases on a project is taking an inordinate amount of time. 在项目上运行测试用例会花费大量时间。 In diagnosing this issue, we have determined that a certain service call is dragging its digital feet. 在诊断此问题时,我们确定某个服务呼叫正在拖延其数字脚。 We would like to mock it out via Spring as we have done in other projects, however this project makes the service call vicariously (it calls a project, who calls a project, who calls the service; each with their own spring configs). 我们希望像其他项目一样通过Spring对其进行模拟,但是该项目会替代性地调用服务(它调用一个项目,然后再调用一个项目,然后再调用该服务;每个都具有自己的spring配置)。

Project Alpha -> Project Bravo -> Service Foo from Project Charlie Project Alpha-> ​​Project Bravo-> Charlie项目的Service Foo

Question: Will modifications to the Spring config in Project Alpha allow mocking out Service Foo or will the Spring config in Project Bravo be unaffected by configurations outside its .war file. 问题:对Project Alpha中的Spring配置进行修改是否可以模拟出Service Foo,还是将Bravo Project中的Spring配置不受其.war文件外部的配置影响。

Note: This could easily be accomplished by placing the mock config in Project Bravo yet that is not an option in this case. 注意:可以通过将模拟配置放置在Project Bravo中来轻松实现,但在这种情况下不可行。

Stone age version 石器时代版

Springockito Springockito

This is a small extension to spring that simplifies way of creation mockito mocks in the intergation tests' related context xml files. 这是对spring的一个小扩展,它简化了集成测试的相关上下文xml文件中的创建模拟副本的方式。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mockito="http://www.mockito.org/spring/mockito"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
...
    <mockito:mock id="innerBean" class="my.project.InnerBean" />
..
</beans>

New Age version 新时代版本

Try @ReplaceWithMock annotation 试试@ReplaceWithMock注释

@ContextConfiguration(loader = SpringockitoContextLoader.class,
locations = "classpath:/context.xml")
public class SpringockitoAnnotationsMocksIntegrationTest extends AbstractJUnit4SpringContextTests {

    @ReplaceWithMock
    @Autowired
    private InnerBean innerBean;

...
}

https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM