2

I'm trying to test a service with Mockito and testNG, but i have a couple of doubts. It's necessary create get/set to inject service, if service is declaredd like this:

  @Autowired(required = true)
    protected ITipService serveiTip;

when I'm trying to clean and package with maven I found this exception:

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?

I believe that with autowiring get/set will be not necessary.

this is my test-context:

?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>

ApplicationContext:

<?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>
StuartLC
  • 104,537
  • 17
  • 209
  • 285
ZaoTaoBao
  • 2,567
  • 2
  • 20
  • 28

1 Answers1

3

Using @Autowired will make spring automatically inject a matching bean into that field. Thus it is no longer required to define the "consultaDeutes" bean in the xml. If you'd like to use the xml definition, I believe you should define a setter for each property that you are trying to inject, eg: serveiTip, serveiGpt.

Using @Autowired in your test might require 2 additional annotation on the definition of your test class:

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

I actually made a mistake pasting the @RunWith annotation specific for JUnit. For TestNG you can lookup this link. Apologies

Community
  • 1
  • 1
Morfic
  • 15,178
  • 3
  • 51
  • 61
  • dou you think? but if need to user consultaDeutes i the web-flow? maybe i don't understand you. – ZaoTaoBao Sep 17 '13 at 07:48
  • If you need to have different stuff in your tests and pro code and keep the same xml file, you can always take advantage of profiles: http://java.dzone.com/articles/using-spring-profiles-xml. Define the "production" profile as default, and in your tests annotate the test class with @ActiveProfiles("dev") or whatever name you choose (I'm adding it to the original example as well) – Morfic Sep 17 '13 at 07:50
  • OK! you are right, in the context if I inject with autowiring i don't need to add property, i post the result end. – ZaoTaoBao Sep 17 '13 at 07:54
  • If still want to have different stuff between test and pro, and you don't mind creating multiple spring config xmls as your example would suggest (applicationContext-gui-deutes-Test.xml) you can chose the one you want for the current test with @ContextConfiguration(value = "/myContext.xml") – Morfic Sep 17 '13 at 07:58