mercredi 1 avril 2015

Mocking a local object inside a method of SUT using Mockito or PowerMocktio

I've class method like below which creates a local object and calls a method on that local object.



public class MyClass {
public someReturn myMethod(){
MyOtherClass otherClassObject = new MyOtherClass();
boolean retBool = otherClassObject.otherClassMethod();
if(retBool){
// do something
}
}
}


When I'm testing myMethod, I want to mock otherClassObject.otherClassMethod to return something of my choice. I know I must have used a factory for MyOtherClass instantiation in this case but it's legacy code and I don't want to change any code now. I see that Mockito doesn't provide this facility to mock MyOtherClass in this case but possible with PowerMockito. However, I could not find an example for above scenario but found only for static class. How should I mock local object inside a method of SUT ?


I also referred to some other OS questions like - Mocking methods of local scope objects with Mockito but they were not helpful.


A code example will be of great help.


Aucun commentaire:

Enregistrer un commentaire