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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -