【问题标题】:Cannot setup Mock Class in Spring无法在 Spring 中设置 Mock Class
【发布时间】:2016-07-17 10:31:06
【问题描述】:

我即将进入一个包含集成测试套件的项目。其中一个测试自动连接了一个类,该类对第三方供应商进行了大量网络调用。因为我们不希望我们的测试进行这些调用,所以团队通过一个 testApplicationContext.xml Spring 配置文件模拟了这个客户端。

集成测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testApplicationContext.xml")
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class IntegrationTest {

testApplication.xml 的定义:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <aop:aspectj-autoproxy />
    <context:component-scan base-package="com.mycompany" />
    <context:component-scan base-package="tst.mycompany.mocks"/>
    <import resource="mock-beans.xml"/>

mock-beans.xml的定义:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"...>
    <!-- Override bean definitions with mocks -->
    <bean id="thirdPartyClientService" class="tst.mycompany.mocks.MockThirdPartyClient" />
</beans>

这在我们的设置中运行良好,当作为测试运行时,一个 MockThirdPartyClient 实例会自动装配到我们的 Spring bean 中。

但是,我最近在同一个包中为另一个服务添加了一个新的模拟,但无法加载它。我得到了错误:

找不到类路径资源 [mock-beans.xml] 中定义的名称为“addressService”的 bean 的类 [tst.mycompany.mocks.MockAdressService];嵌套异常是 java.lang.ClassNotFoundException: tst.mycompany.mocks.MockAdressService

这是更新后的 mock-beans.xml

<beans xmlns="http://www.springframework.org/schema/beans">
    <!-- Override bean definitions with mocks -->
    <bean id="thirdPartyClientService" class="tst.mycompany.mocks.MockThirdPartyClient" />
    <bean id="addressService" class="tst.mycompany.mocks.MockAdressService" />
</beans>

以下是我如何自动装配有效的初始依赖项:

@Autowired ThirdPartyClientServiceI client;

还有一个没有:

@Autowired AddressServiceI addressService;

我已经仔细检查了所有内容的拼写,所有内容都排成一行,所以这不起作用真是太疯狂了。我以为可能是某些东西被错误地缓存了……但是我刷新并清理了所有东西,仍然没有骰子。

这些模拟都没有 FWIW 注释,它们只是实现了它们旨在模拟的服务的接口。它们都位于src/test/java 文件夹下。

package tst.mycompany.mocks;

public class MockAddressService implements AddressServiceI {

有问题的堆栈跟踪:

Caused by: java.lang.ClassNotFoundException: tst.mycompany.mocks.MockAdressService
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_71]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_71]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[?:1.8.0_71]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_71]
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.2.2.RELEASE.jar:4.2.2.RELEASE]

我确实检查了 bin 文件夹,编译的类在那里。

【问题讨论】:

  • 我不知道发生了什么,但有一个小小的猜测:你正在用你的模拟“覆盖”和现有的地址服务......你的真实 bean 有相同的 id 吗? bean id="addressService"
  • 实现 AddressServiceI 的“真实”bean 称为 AddressService 并自动装配为:@Autowired AddressServiceI addressService。我尝试修改 mock-beans.xml 中的 id 但这并没有解决它。我还尝试在绝望中更改addressService字段实例变量的名称,但无济于事。

标签: spring mocking springjunit4classrunner


【解决方案1】:

这很尴尬,但我在 mock-beans.xml 中的 bean 定义中有一个错字: MockAdressService 应该是 MockAddressService(缺少“d”)

【讨论】:

    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多