I have the following code, based off the documentation provided by Realm (http://ift.tt/1SNAQlg)
public Observable<Foo> getFooById(String id) {
realm = Realm.getInstance(realmConfiguration);
return realm.where(Foo.class)
.equalTo("id", id)
.findFirstAsync()
.asObservable()
.filter(this::filterResult);
}
This works in App as expected however when it comes to testing things become a little more tricky. I have the following test (stripped down to keep it simple) :
@Test
public void testRealmExample() {
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name("test.realm")
.inMemory()
.build();
DataManager dataManager = new DataManager(config);
TestSubscriber<Foo> testSubscriber = new TestSubscriber<>();
dataManager.getFoo("").observeOn(AndroidSchedulers.mainThread()).subscribe(testSubscriber);
testSubscriber.assertNoErrors();
}
When the test is executed the following error occurs java.lang.IllegalStateException: Your Realm is opened from a thread without a Looper. Async queries need a Handler to send results of your query
.
To counter this I read on the Realm GitHub page that they use an annotation @UiThreadTest
to force the test to run on the UI thread, which from my understanding is a looper thread, therefore this should solve my problem. I added:
@Rule
public final UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();
and changed my test to include the annotation
@Test
@UiThreadTest
public void testRealmExample() { ...}
This still yields the same exception. Does anyone know why and a solution? Thanks.
Aucun commentaire:
Enregistrer un commentaire