java - Spring unit test objects autowired with null fields -


i'm attempting create unit tests rest service in spring servlet. when controller object created @autowire, of @autowired fields null.

my test class follows, using springjunit runner , context configuration set

@contextconfiguration(locations = "examplerestcontrollertest-context.xml") @runwith(springjunit4classrunner.class) public class examplerestcontrollertest {      @autowired     private baseservice mockexampleservice;      @autowired     private examplerestcontroller testexamplerestcontroller; 

the examplerestcontrollertest-context.xml sets service mocked , injects mocked object controller

<context:annotation-config/>  <import resource="classpath*:example-servlet.xml"/>  <bean id="mockexampleservice" class="org.easymock.easymock" factory-method="createmock">     <constructor-arg index="0" value="za.co.myexample.example.services.baseservice"/> </bean> <bean id="testexamplerestcontroller" class="za.co.myexample.example.rest.controller.examplerestcontroller">     <property name="exampleservice" ref="mockexampleservice"/> </bean> 

the rest of beans used controler defined in example-servlet.xml

<bean id="restcodemapper" class="za.co.myexample.example.rest.util.restcodemapper"/>  <bean id="restproperties" class="org.springframework.beans.factory.config.propertiesfactorybean">         <property name="location" value="classpath:restservicecodemapping.properties"/> </bean> 

along jax2bmarshaller.

my ide links these definitions fine , if remove of definitions "no qualifying bean" error expected.

my problem when run unit tests controller provided has of fields nulls

@controller public abstract class baserestcontroller {      private static logger logger = logger.getlogger(baserestcontroller.class);     protected final string headers = "content-type=application/json,application/xml";     @autowired     protected restcodemapper restcodemapper;      @autowired     protected baseservice exampleservice;      @autowired     protected jaxb2marshaller jaxb2marshaller; 

(and implementing class)

@controller @requestmapping("/example") public class examplerestcontroller extends baserestcontroller { 

when run proper code in weblogic server fields populated. more if add these fields in test class directly fields @autowired correctly. @autowire objects in test class , set them directly controller. that's not right way of doing it.

so question is, why autowired fields of autowired object null in test class when autowires objects directly in same test class or if run code on weblogic server.

in test class, @autowired objects null because of context configuration, change to:

@contextconfiguration(locations = "examplerestcontrollertest-context.xml") 

to

@contextconfiguration(locations = "classpath:/examplerestcontrollertest-context.xml") 

in examplerestcontrollertest

so question is, why autowired fields of autowired object null in test class when autowires objects directly in same test class or if run code on weblogic server.

they must null in autowired objects' constructor. can try create @postconstruct method in autowired object , in method autowired objects must not null


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -