vendredi 8 mai 2015

Mock a method call from another class

My code structure :

class A{
void methodA(){
//some code
    B b = new B();
    response = b.methodB(arg1, arg2);
   //some code using "response"
 }
}

I am UNIT testing class A and don't want to actually call methodB(). Is there any way to mock this method call by a custom response. I tried Mockito to mock this method call as below:

B classBMock = Mockito.mock(B.class);
Mockito.when(classBMock.methodB(arg1, arg2)).thenReturn(customResponse);
A objA = new A();
objA.methodA();

On calling methodA() the above way I don't get customResponse when methodB() is called within A. But when I call methodB() with classBMock, I get the customResponse. Is there anyway I can get customResponse from methodB() while calling methodA().

Aucun commentaire:

Enregistrer un commentaire