简体   繁体   中英

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).

Project Alpha -> Project Bravo -> Service Foo from Project Charlie

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.

Note: This could easily be accomplished by placing the mock config in Project Bravo yet that is not an option in this case.

Stone age version

Springockito

This is a small extension to spring that simplifies way of creation mockito mocks in the intergation tests' related context xml files.

<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

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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