java - Mockito: mock method parameter -
i using mockito framework testing. having following problem.
this class want test.
@service public class parent{ @autowired private child child; public void setchilddetails(childdetails childdetails){ childdetails.validate(); .... int age = childdetails.getage(); } }
this test.
public class parenttest { @injectmocks private parent parent; @mock private child child; @before public void setup() { initmocks(this); } @test public testparentmethod(){ ..... } @test public testchilddetailsmethod(){ childdetails childdetails = new childdetails(); childdetails.setage(20); parent.setchilddetails(childdetails); } }
mock child property job. problem want mock childdetails.validate() (only method) , leave other methods (getage() must return 20). can suggest how resolve problem?
for purpose should use spy
. spy partual mock , mock individual methods tell to. if validate()
void
method, can use donothing()
method spy. use below:
@test public testchilddetailsmethod(){ childdetailsspy childspy = spy(new childdetailsspy()); donothing().when(childspy).validate(); childdetails.setage(20); ... }
more reading subject can found here.
Comments
Post a Comment