EDIT
When reading the FAQ, it gave me some idea about what could possibly cause an issue here. Just to give it a try, I changed the visibility of the stubbed method open() to public and it was executed as expected, without any exception thrown.
I'm not sure if it's a bug or the desired behaviour of version 1.10.19.
ORIGINAL POST
In my Android project, I'm using Mockito to ease the implementation of some (instrumentation) tests. I was able to mock some non-void methods, but didn't figured out how to properly stub a void method.
I'm trying to test a class House. A House has an attribute of type Door and a method openDoor(). A Door and an attribute of type Handle and a method open(). When I invoke the openDoor(), I would like to check if open() was called, so I wrote this code:
@Test
public void testOpenDoorInitial() {
Door stubbedDoor = mock(Door.class);
doNothing().when(stubbedDoor).open();
myHouse.setDoor(stubbedDoor); //myHouse has been initialized
myHouse.openDoor();
verify(stubbedDoor, times(1)).open();
}
public class House {
Door door;
//rest of code
void setDoor(Door d){
door = d;
}
void openDoor(){
// some conditions
door.open();
}
}
public class Door {
Handle handle;
//... rest of code
void open(){
handle.tryToUse(); //Throws NullPointException
}
}
The problem is that a NullPointerException is thrown on line doNothing.when(stubbedDoor).open();, telling me that handle is null. doNothing() seems to actually call open(), which I don't expect.
Does anyone has an idea about the source of this problem ? I'm new to Mockito, so I could have missed something obvious.
To enable Mockito in instrumentation testing, I imported the following modules.
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"
Aucun commentaire:
Enregistrer un commentaire