realm-java: ISSUE - Unit test Problem (librealm-jni.so)
When i want to run this simple unit test :
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareForTest({Realm.class, RealmLog.class})
public class ExampleUnitTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
Realm mockRealm;
@Before
public void setup() throws Exception {
mockStatic(Realm.class);
mockStatic(RealmLog.class);
Realm mockRealm = PowerMockito.mock(Realm.class);
when(Realm.getDefaultInstance()).thenReturn(mockRealm);
when(Realm.getDefaultInstance()).thenReturn(mockRealm);
this.mockRealm = mockRealm;
}
@Test
public void shouldBeAbleToGetDefaultInstance() {
assertThat(Realm.getDefaultInstance(), is(mockRealm));
}
}
I face this error :
java.lang.UnsatisfiedLinkError: Can't load library: /tmp/android-tmp-robolectric2838690925023503406/app_lib/librealm-jni.so
and indicates to the error line in my class that extends from Application where i init the realm :
Realm.init(getApplicationContext());
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build();
Realm.setDefaultConfiguration(realmConfiguration);
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 33 (18 by maintainers)
I ran into the same / similar issue. After upgrading to Realm 2.x, a lot of robolectic tests failed. The root cause was the
Realm.init
addition to theApplication
.Since I didn’t want to touch any of the tests, I ended up using reflection to make
Realm.init
think it is already initialized. SettinglibraryIsLoaded
to true was not enough - it was still causing exceptions.Thoughts on this approach? For me the above mentioned mock approach with mockStatic/PrepareForTest only worked with
PowerMockRule
and not withMockitoRule
.Here is the code snippet to fake init Realm.
@erudonja1 Robolectric claims that they’ve fixed the classloader issue that broke Powermock in your case in
3.3.1
see https://github.com/robolectric/robolectric/issues/2944
Ah, you should create a package named
rx
, and you should create an empty class inside it calledObservable
The only way to work around it for now is set
RealmCore#libraryIsLoaded
totrue
before callingRealm#init(Context)
by using reflection.