如何使用Roboelectric在Realm Db中运行单元测试 [英] How to run Unit test in Realm Db using Roboelectric

查看:110
本文介绍了如何使用Roboelectric在Realm Db中运行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个用于测试Realm db的简单测试用例.但我总是出错

I have written a simple test case using for testing Realm db. But I'm always getting error

java.lang.UnsatisfiedLinkError: Can't load library: /tmp/android-tmp-robolectric4167396756480823821/app_lib/librealm-jni.so

这是我的考验

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import io.realm.Realm;

@RunWith(RobolectricTestRunner.class)
public class RealmTest {

@Test
public void realmShouldWork() {
    Realm realm = Realm.getInstance(RuntimeEnvironment.application.getApplicationContext());
 }
}

我正在Application类中设置Realm配置.在正常运行模式下,它可以正常工作.问题仅在测试模式下.我无法在测试模式下初始化Realm数据库实例.

I'm setting up the Realm config in the Application class. And in normal run mode it works fine. Problem is only while in test mode. I am not able to init the Realm db instance in test mode.

推荐答案

在否决票抱怨没有代码可以解决您的问题或信息不足的情况下(如果链接消失了一天),我写了一个库来支持测试Realm使用PowerMockito.请查看该库,因为它可以帮助您进行测试 https://github.com/juanmendez/Mocking-领域.当您访问Wiki时,您将找到如何通过简单测试或Robolectric使用它.它已经在Realm 3.0中进行了测试,但是除非有很大的变化,否则您可以将其与最新版本一起使用,您可以告诉我.谢谢.

Before negative votes complaining there is no code to solve your problem or not sufficient information in case the link is gone some day, I wrote a library to support testing Realm using PowerMockito. Please see the library as it can help you do your tests https://github.com/juanmendez/Mocking-Realm. You will find how to use it with simple testing or with Robolectric when you go to the wiki. It was done testing it with Realm 3.0, but you can use it with the latest version unless something has greatly changed, you can let me know. Thanks.

这篇关于如何使用Roboelectric在Realm Db中运行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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