spring - @Autowired with JUnit tests -



spring - @Autowired with JUnit tests -

i´ve using junit tests have issues, these tests have @autowired annotation within spring beans , when reference them beans @autowired null.

here illustration code:

public class test { protected applicationcontext ac; @before public void setup() { ac = new filesystemxmlapplicationcontext("classpath:config/applicationcontext.xml" "classpath:config/test-datasources.xml"); } @test public void testrun() throws exception { imanager manager = (imanager)this.ac.getbean("manager"); manager.dosomething(); } } @service public class manager implements imanager { public boolean dosomething() throws exception { parametersjcscache parametersjcscache = new parametersjcscache(); string paramvalue = parametersjcscache.getparameter("xpto"); ... } } public class parametersjcscache extends springbeanautowiringsupport { @autowired private iparametermanager parametermanager; //this null }

when invoking manager object spring generates proxy when accessing @autowired parametermanager object null, , issue can´t test method.

any 1 have thought causing this? why object don´t injected? works in context of web application, in test context null.

cheers,

bigster

looks me parametersjcscache not spring managed bean. need create spring bean , autowire manager

@service public class manager implements imanager { public boolean dosomething() throws exception { parametersjcscache parametersjcscache = new parametersjcscache(); <-- create new (non spring-managed) instance string paramvalue = parametersjcscache.getparameter("xpto"); ... } }

change to:

@service public class manager implements imanager { @autowired private parametersjcscache parametersjcscache; public boolean dosomething() throws exception { string paramvalue = parametersjcscache.getparameter("xpto"); ... } } @component public class parametersjcscache extends jcscache { @autowired private iparametermanager parametermanager; //this null }

spring junit autowired

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -