java - How to unit test method which invokes another method? -


how test method invokes method? in example know how test initcomponentstypea invokes create method.

public myclass{  private list<typea> componentstypea;  public void initcomponents(config c){        componentstypea = initcomponentstypea(c);     //... }  private list<typea> initcomponentstypea(config c){     //...     list<myobject> somelist = c.getsomelist();     list<typea> locallist = new arraylist<>();     for(myobject mo : somelist){         locallist.add(create(mo));     }     return locallist; }  private typea create(myobject myobject){     // ... } } 

i know 1 solution refactor code in way (show below). necessary? solution?

public myclass{  private list<typea> componentstypea;  public void initcomponents(config c){         list<myobject> myobjectlist = initcomponentstypea(c);      componentstypea = create(myobjectlist)     //... }  private list<myobject> initcomponentstypea(config c){     //...     list<myobject> somelist = c.getsomelist();     return somelist; }  private list<typea> create(list<myobject> myobjectlist){     // ... } } 

in second, refactored example names of methods should changed, accordingly meaning.

you can use mockito , either create mock return real methods, override behaviour method want mock, or use spy, creates mock wraps real instance of object.

here's tutorial on mockito spy.

edit:

since method mocked private, , @fge mentioned, need mocking library exposes private method. , powermock, that.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -