Mockito打破Android> = 5.0但不支持Android 4.4

时间:2016-04-20 16:34:04

标签: java android mockito android-5.0-lollipop android-4.4-kitkat

所以我有一个适用于Android 4.4.4的测试套件。但是,当我针对Android 5.0.0或Android 6.0.0进行测试时,Mockito开始给我一系列不同的问题。我使用的是Mockito v1.9.5,但尝试了1.10.192.0.49-beta但错误相同。例如,我的设置代码中包含以下内容:

import static org.mockito.Mockito.*;

...

MyClass myClass = mock(myClass.class);
when(myClass.myMethod()).thenReturn(5L);

类方法看起来像

class MyClass {
    private long myPrivateVariable;

    synchronized long myMethod() {
        return myPrivateVariable
    }
}

在Android 4.4.4上运行正常但在Android 5.0.0上我遇到以下错误:

org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
It is a limitation of the mock engine.

另一个例子是在一个案例中我正在嘲笑一个类并调用一个方法

AnotherClass anotherClass = mock(AnotherClass.class)

但是,当调用该类的方法时,它会引发NullPointerException,因为该类的私有属性未设置且公共方法需要它。

class AnotherClass {
    private PrivateAttribute privateAttribute;

    synchronized Map<String, Object> getAttribute(SomeClass instance) {
        return privateAttribute.getSomething(instance);
    }
}

这会在Android 5.0.0和Android 6.0.0中引发NullPointerException,但不会在Android 4.4.4中引发

0 个答案:

没有答案