junit - 未定义 [org.springframework.ws.client.core.WebServiceTemplate] 类型的唯一 bean

标签 junit

我正在尝试使用类org.springframework.ws.client.core.WebServiceTemplate制作两个测试用例。两个测试用例都属于不同的类,所以我为它们制作了两个不同的 bean。

在运行 junit 测试时,我遇到了这样的错误

Error creating bean with name 'testcases.TestAdminMethodsWebService': Unsatisfied dependency expressed through bean property 'admin': : No unique bean of type [org.springframework.ws.client.core.WebServiceTemplate] is defined: expected single matching bean but found 7: [admin, rules]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.ws.client.core.WebServiceTemplate] is defined: expected single matching bean but found 2: [admin, rules]

我的bean是这样的:

<oxm:jaxb2-marshaller id="marshaller_admin" contextPath="a.com.b" />
<bean id="admin" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="marshaller" ref="marshaller_admin" />
    <property name="unmarshaller" ref="marshaller_admin" />
    <property name="defaultUri"
        value="http://dev05:8080/.." />
</bean>
<oxm:jaxb2-marshaller id="marshaller_rules" contextPath="r.com.b" />
<bean id="rules" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="marshaller" ref="marshaller_rules" />
    <property name="unmarshaller" ref="marshaller_rules" />
    <property name="defaultUri"
        value="http://dev05:8080/.." />
</bean>

请告诉我如何克服这个问题或者为什么会发生这个错误,任何帮助将不胜感激,谢谢。

最佳答案

使用@Qualifier注解来帮助Spring确定应该注入(inject)哪个bean。

public class TestClass {

    @Autowired
    @Qualifier("admin")
    WebServiceTemplate admin;

    @Autowired
    @Qualifier("rules")
    WebServiceTemplate rules;

    // ... Rest of your class

}

阅读文档 here在使用限定符微调基于注释的 Autowiring 部分下。

更新:

您还需要像这样更改 xml bean 定义:

<oxm:jaxb2-marshaller id="marshaller_admin" contextPath="a.com.b" />
<bean class="org.springframework.ws.client.core.WebServiceTemplate">
    <qualifier value="admin"/>
    <property name="marshaller" ref="marshaller_admin" />
    <property name="unmarshaller" ref="marshaller_admin" />
    <property name="defaultUri"
        value="http://dev05:8080/.." />
</bean>
<oxm:jaxb2-marshaller id="marshaller_rules" contextPath="r.com.b" />
<bean class="org.springframework.ws.client.core.WebServiceTemplate">
    <qualifier value="rules"/>
    <property name="marshaller" ref="marshaller_rules" />
    <property name="unmarshaller" ref="marshaller_rules" />
    <property name="defaultUri"
        value="http://dev05:8080/.." />
</bean>

请注意包含 <qualifier>每个 bean 定义下的标记。

关于junit - 未定义 [org.springframework.ws.client.core.WebServiceTemplate] 类型的唯一 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10797566/

相关文章:

java - 如何将 jUnit 5 与 Gradle 结合使用

java - 如何测试一个方法是否被调用?

java - 如何测试多个服务调用

java - JUnit 测试用例使用 Mockito : null pointer exception

java - 将参数从 JUnit 5 @BeforeEach 传递到单独的测试

java - Surefire 由于检查是否在 junit 测试之一中调用了 System.exit() 而失败

java - 如何测试任何方法调用是否抛出异常?

java - 如何从属性或 xml 文件填充 SOAP 请求参数?

java - 我的单元测试中出现 NullPointerException

java - 如何在通过JUnit测试的多线程应用程序中调试断点?