2

我正在尝试使用 Mockito 和 testNG 测试服务,但我有几个疑问。如果服务是这样声明的,则必须创建 get/set 来注入服务:

  @Autowired(required = true)
    protected ITipService serveiTip;

当我试图用 maven 清理和打包时,我发现了这个异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consultaDeutes' defined in URL
[file:/D:/workspaceGPT/GPT/gpt.ui/target/test-classes/applicationContext-gui-deutes-Test.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'serveiTip' of bean class [cat.base.gpt.ui.ConsultaDeutesTest]: Bean property 'serveiTip' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我相信自动装配 get/set 是没有必要的。

这是我的测试环境:

?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.mockito.org/spring/mockito https://bitbucket.org/kubek2k/springockito/raw/tip/springockito/src/main/resources/spring/mockito.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="cat.base.gpt.ui" />


    <!-- mock del serveis que podem atacar per solicitar info -->
    <mockito:mock id="serveiSubjecte" class="cat.base.tip.service.ISubjectesService"/>
    <mockito:mock id="serveiTip" class="cat.base.tip.service.ITipService"/>
    <mockito:mock id="serveiGpt" class="cat.base.gpt.domini.service.IGptService"/>
    <mockito:mock id="sessio" class="cat.base.baseframe.session.IBaseSession"/>
    <mockito:mock id="usuari" class="cat.base.baseframe.user.IBaseUser"/>

    <!--  
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:cat/base/bfp/ui/applicationResources" />
    </bean>
    -->

    <bean name="consultaDeutes" class="cat.base.gpt.ui.ConsultaDeutesTest">
        <property name="serveiTip" ref="serveiTip"/>
        <property name="serveiGpt" ref="serveiGpt"/>
    </bean>

</beans>

应用上下文:

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.mockito.org/spring/mockito https://bitbucket.org/kubek2k/springockito/raw/tip/springockito/src/main/resources/spring/mockito.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="cat.base.gpt.ui" />


    <!-- mock del serveis que podem atacar per solicitar info -->
    <mockito:mock id="serveiSubjecte" class="cat.base.tip.service.ISubjectesService"/>
    <mockito:mock id="serveiTip" class="cat.base.tip.service.ITipService"/>
    <mockito:mock id="serveiGpt" class="cat.base.gpt.domini.service.IGptService"/>
    <mockito:mock id="sessio" class="cat.base.baseframe.session.IBaseSession"/>
    <mockito:mock id="usuari" class="cat.base.baseframe.user.IBaseUser"/>

    <!--  
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:cat/base/bfp/ui/applicationResources" />
    </bean>
    -->

    <bean name="consultaDeutes" class="cat.base.gpt.ui.ConsultaDeutesTest"/>
        <!-- WITH OUT PROPERTIES!!-->
</beans>
4

1 回答 1

3

使用 @Autowired 将使 spring 自动将匹配的 bean 注入该字段。因此不再需要在 xml 中定义“consultaDeutes”bean。如果您想使用 xml 定义,我相信您应该为您尝试注入的每个属性定义一个 setter,例如:serveiTip、serveiGpt。

在测试中使用 @Autowired 可能需要在测试类的定义上添加 2 个附加注释:

@ContextConfiguration(value = "/myContext.xml")
//@RunWith(SpringJUnit4ClassRunner.class) This is JUnit specific
@ActiveProfiles("dev")
public class TestCompareService {
    @Autowired(required = true)
    protected ITipService serveiTip;
    ....
}

实际上,我在粘贴特定于 JUnit 的 @RunWith 注释时犯了一个错误。对于 TestNG,您可以查找此链接。道歉

于 2013-09-17T07:44:53.963 回答