java - @Autowired is not working with beans defined in application context file -
i defined bean in spring context file 'applicationcontext.xml' below :
<bean id="daobean" class="org.mockito.mockito" factory-method="mock"> <constructor-arg value="com.xxx.daoimpl" /> </bean> in service class (serviceimpl), using bean below:
@component("serviceimpl") public class serviceimpl{ // other code here @autowired private transient daoimpl daobean; // other code here } my service class being accessed junit test class.
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = { "/applicationcontext.xml" }) public class junittest{ // other code here @autowired private transient serviceimpl serviceimpl; // test cases here } when execute test case, gives error saying:
error creating bean name 'serviceimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private transient com.xxx.daoimpl
when remove @autowired service class , use @resource(name = "daobean") test case works fine.
public class serviceimpl{ // other code here @resource(name = "daobean") private transient daoimpl daobean; // other code here } my question why @autowired not working in case? need configure thing else @autowired, can work properly. don't want change service layer classes replace @autowired @resource.
mockito.mock() has generic return type t erased @ runtime, spring cannot infer type of created mock registered object in spring context. that's why @autowired doesn't work (as tries dependency type).
check out this answer solution problem.
Comments
Post a Comment