Android上的领域单元测试 [英] Realm Unit Test on Android

查看:85
本文介绍了Android上的领域单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试对对Realm(0.87.4)进行某些调用的类进行单元测试,则测试设置失败,并显示

Trying to unit test a class that does some calls to Realm (0.87.4), the test setup fails with

java.lang.NoClassDefFoundError: rx/Observable$OnSubscribe
at io.realm.RealmConfiguration$Builder.<init>(RealmConfiguration.java:279)
at org.testapp.db.MyClassTest.setUp(MyClassTest.java:34)
... 
Caused by: java.lang.ClassNotFoundException: rx.Observable$OnSubscribe
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

我的测试课开始于:

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest extends TestCase {

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();

Realm realm;

@Before
public void setUp() throws Exception {
    File tempFolder = testFolder.newFolder("realmdata");
    RealmConfiguration config = new RealmConfiguration.Builder(tempFolder).build();

    realm = Realm.getInstance(config);
}
...

我的毕业证书有:

testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.10.19"
testCompile "org.robolectric:robolectric:3.0"
compile 'io.realm:realm-android:0.87.4'

如何解决这个问题?

===编辑1 ===

我添加了我的gradle:

I added to my gradle:

testCompile 'io.reactivex:rxjava:1.1.0'

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}

新错误是

java.lang.UnsatisfiedLinkError: no realm-jni in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at io.realm.internal.RealmCore.loadLibrary(RealmCore.java:117)

推荐答案

在要测试的类中使用Realm时,单元测试很难进行或无法进行(感谢Dmitry提及).我所能做的就是将测试作为工具测试进行运行(感谢Dmitry,Christian).

Unit tests are hard or impossible when using Realm in the class that you are testing (thanks Dmitry for mentioning). What I can do is run the tests as instrumental tests (thanks Dmitry, Christian).

这很容易,我无需更改测试方法...

And that is quite easy, I won't have to change anything to the test methods...

A.将测试类移到"androidTest"文件夹中,而不是"test"中. (从Android Studio 1.1开始,您应该将单元测试放在/src/test中,而将Android Instrumentation测试放在/src/androidTest中)

A. Move the test class into an "androidTest" folder, instead of "test". (Since Android Studio 1.1 you should put your Unit tests in /src/test and Android Instrumentation Tests in /src/androidTest)

B.在gradle构建文件中添加工具测试的依赖项,请使用"androidTest",因为它们是工具性的:

B. Add the dependencies for instrumental tests in the gradle build file, use "androidTest" because they're instrumental:

androidTestCompile 'junit:junit:4.12'
androidTestCompile 'io.reactivex:rxjava:1.1.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:support-annotations:23.1.1'

C.在测试类中,将顶部的运行器替换为AndroidJUnit4:

C. In the test class, replace the runner at the top with AndroidJUnit4:

@RunWith(AndroidJUnit4.class)
public class MyClassTest extends TestCase {
...

创建类型为"Android测试"的Android运行配置,然后运行它,瞧,它现在可以在设备上测试相同的方法了.让我很高兴.

Create an Android run configuration of type "Android Tests", run it and voila, it will test the same methods fine now, but on a device. Makes me very happy.

这篇关于Android上的领域单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆