Sunday 15 June 2014

java - How to use Mockito for composite object -



java - How to use Mockito for composite object -

i want mock dao manager this

public class daomanager{ @autowired private service1 service; @autowired private service2 service 2; @autowired private daomanager1 manager 1; public customerdetail getcustomerdetails(){ manager1.getcustomerdetails(); } public class daomanager1{ @autowired private service3 service3; @autowired private service4 service 4; public getcustomerdetails(){ service3.getcustname(); service4.getcustaddress(); }

my question how mock daomanager class? if mock it, need mock each , every manager/service gets called getcustomerdetails method? looks big overhead me. ideas or mebbe getting wrong?

edit:

when run junit , next error.

caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'mockdaomanager': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private x.y.z.service2 x.y.z.service2; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [x.y.z.service2] found dependency: expected @ to the lowest degree 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}

you have daomanager contract in interface , mock last

interface idaomanager { customerdetail getcustomerdetails(); } public class daomanager implements idaomanager

then follow nsanglar advises

idaomanager daomanagermock = mock(idaomanager.class); [...]

of course of study have inject dao using interface, is, anyway, practice

java unit-testing mocking mockito junit4

No comments:

Post a Comment