[英]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. 只是尝试一下,我将stubbed方法
open()
的可见性更改为public ,并按预期执行,没有任何异常抛出。
I'm not sure if it's a bug or the desired behaviour of version 1.10.19. 我不确定这是一个错误还是版本1.10.19所需的行为。
ORIGINAL POST 原始邮政
In my Android project, I'm using Mockito to ease the implementation of some (instrumentation) tests. 在我的Android项目中,我使用Mockito来简化某些(仪器)测试的实现。 I was able to mock some non-void methods, but didn't figured out how to properly stub a void method .
我能够模拟一些非void方法,但没有弄清楚如何正确地存根void方法 。
I'm trying to test a class House
. 我正试图测试一个班级的
House
。 A House
has an attribute of type Door
and a method openDoor()
. House
具有类型为Door
的属性和方法openDoor()
。 A Door
and an attribute of type Handle
and a method open()
. 一个
Door
和一个Handle
类型的属性和一个方法open()
。 When I invoke the openDoor()
, I would like to check if open()
was called, so I wrote this code: 当我调用
openDoor()
,我想检查open()
被调用,所以我写了这段代码:
@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();
问题是在
doNothing.when(stubbedDoor).open();
行上抛出NullPointerException
doNothing.when(stubbedDoor).open();
, telling me that handle
is null. 告诉我
handle
为空。 doNothing() seems to actually call open()
, which I don't expect. doNothing()似乎实际上调用
open()
,这是我不期望的。
Does anyone has an idea about the source of this problem ? 有没有人对这个问题的根源有所了解? I'm new to Mockito, so I could have missed something obvious.
我是Mockito的新手,所以我可能错过了一些明显的东西。
To enable Mockito in instrumentation testing, I imported the following modules. 为了在仪器测试中启用Mockito,我导入了以下模块。
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"
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. 这可能与Mockito的问题212有关 ,其中包私有父类可能导致模拟失败,因为Mockito无法存根隐藏的方法。 (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 ; Mockito 2.0通过从CGLIB切换到ByteBuddy解决了这个问题; I don't remember whether ByteBuddy was a part of any 1.x release.
我不记得ByteBuddy是否是任何1.x版本的一部分。 However, you're using Mockito with DexMaker instead, which may have a similar problem.
但是,你正在使用Mockito和DexMaker,这可能有类似的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.