简体   繁体   中英

Mockito : doNothing tries to invoke the void method in Android instrumented test

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" 

Try to use a newer version, you are using 1.10.19. I am not sure but it seems this issue was solved after, as you can see here . Here you can find the version list.

This may be related to Mockito's issue 212 , in which package-private parent classes can cause mocks to fail because Mockito couldn't stub the hidden methods. (This may be related to synthetic methods that the compiler introduces to work around visibility complications in the class hierarchy.)

Mockito 2.0 solves this problem by switching from CGLIB to ByteBuddy ; I don't remember whether ByteBuddy was a part of any 1.x release. However, you're using Mockito with DexMaker instead, which may have a similar problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM