java - Using Mockito to mock a class method inside another class -


i'm trying write unit tests mockito / junit function this:

class1 {  method {   object1 = class2.method // method want fake return value   // code still want run  } } 

is there way in mockito stub result of class2.method? i'm trying improve code coverage class1 need call real production methods.

i looked mockito api @ spy method overwrite whole method , not part want.

i think understanding question. let me re-phrase, have function trying test , want mock results of function called within function, in different class. have handled in following way.

public myunittest {     private static final myclass2 class2 = mock(myclass2.class);      @begin     public void setuptests() {         when(class2.get(1000)).thenreturn(new user(1000, "john"));         when(class2.validateobject(anyobj()).thenreturn(true);     }      @test     public void testfunctioncall() {         string out = myclass.functioncall();         assertthat(out).isequalto("output");     } } 

what doing within function wrapped @before annotation, setting how want functions in class2 respond given specific inputs. then, within actual test, calling function trying test in class want test. in case, myclass.functioncall() running through normal , not overwriting of methods, mocking outputs gets methods (or method) within myclass2.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -