模拟RealmObject类时出错

时间:2018-04-03 10:00:35

标签: android-studio realm mockito

在Mockito中测试时,我无法理解如何使用模型Realm的类。如果该类继承自RealmObjec

public class Foo extends RealmObject {

@PrimaryKey
private String PK;

public Stuff getStuff(){
    return new Stuff();
}

public int getNum(){
    return 1;
}
}

以模拟方式包装以进行测试

@Test
public void clearObject(){
    Foo mock = mock(Foo.class);
}

即使我包裹间谍

 @Test
public void SpyOnRealmObject(){
    Foo foo = new Foo();
    Foo spy_foo = spy(foo);
}

运行测试时出错:

    org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class ru.arvalon.coffeeorder.trash.Foo.

If you're not sure why you're getting this error, please report to the mailing list.


Java               : 1.8
JVM vendor name    : JetBrains s.r.o
JVM vendor version : 25.152-b02
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 1.8.0_152-release-1024-b02
JVM info           : mixed mode
OS name            : Windows 8.1
OS version         : 6.3


You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.

Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [class java.lang.Object, class ru.arvalon.coffeeorder.trash.Foo, class io.realm.RealmObject, interface io.realm.RealmModel, interface io.realm.internal.ManagableObject]

at ru.arvalon.coffeeorder.mainpage.RealmCheck.RealmObject(RealmCheck.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

Caused by: org.mockito.exceptions.base.MockitoException: Could not modify all classes [class java.lang.Object, class ru.arvalon.coffeeorder.trash.Foo, class io.realm.RealmObject, interface io.realm.RealmModel, interface io.realm.internal.ManagableObject]
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
... 28 more

build.gradle中的部分没有帮助

android {
testOptions {
    unitTests.all {
        jvmArgs '-noverify'
    }
}
}

0 个答案:

没有答案