gpt4 book ai didi

java - Spring:重写/模拟依赖的依赖

转载 作者:行者123 更新时间:2023-11-29 08:53:58 27 4
gpt4 key购买 nike

在项目上运行测试用例会花费过多的时间。在诊断此问题时,我们已确定某个服务调用正在拖延其数字化脚步。我们想像在其他项目中所做的那样通过 Spring 模拟它,但是这个项目替代地进行服务调用(它调用一个项目,谁调用一个项目,谁调用服务;每个都有自己的 spring 配置)。

Alpha 项目 -> Bravo 项目 -> Charlie 项目的服务 Foo

问题:对 Project Alpha 中的 Spring 配置的修改是否允许模拟 Service Foo,或者 Project Bravo 中的 Spring 配置是否不受其 .war 文件外部配置的影响。

注意:这可以通过将模拟配置放在 Project Bravo 中轻松实现,但在这种情况下这不是一个选项。

最佳答案

石器时代版本

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>

新时代版

试试@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

关于java - Spring:重写/模拟依赖的依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21315016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com