问题
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
This is my test
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());
}
}
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.
回答1:
Realm doesn't support Robolectric yet: https://github.com/realm/realm-java/issues/904
But if you want to stub out Realm to use Robolectric you can see how in our unit test example here: https://github.com/realm/realm-java/tree/master/examples/unitTestExample
回答2:
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.
来源:https://stackoverflow.com/questions/36715273/how-to-run-unit-test-in-realm-db-using-roboelectric