Issue with Realm Unit Test on Android

爱⌒轻易说出口 提交于 2019-12-11 18:07:29

问题


Using the JUnit4 Test Runner, the test runs and seems to work, but the query returns no results:

@RunWith(AndroidJUnit4::class)
class LocationViewInstrumentationTest {

@Rule
public val mActivityRule: ActivityTestRule<MapsActivity> = ActivityTestRule(MapsActivity::class.java)

@Rule
var testFolder = TemporaryFolder()


@Test
fun mapViewIsRendered() {
    onView(withId(R.id.map)).check(matches(isDisplayed()))
}

@Test
@Throws(IOException::class)
fun canSaveLocation() {
    val tempFolder = testFolder.newFolder("realmdata")
    val config = RealmConfiguration.Builder(tempFolder).build()
    val realm = Realm.getInstance(config)

    realm.beginTransaction()
    val location = Location("Poppy Manor", 33.2, -121.3, 0.0)

    assertThat(location, not(nullValue()))

    realm.commitTransaction()

    RealmQuery<Location> query = realm.where(Location.class);
    RealmResults<Location> results = query.findAll();
    assertThat(results.size(), equalTo(1));

}

Yes I looked at the example project but do not want to add all the dependencies and want my tests to be readable so trying to avoid all the mocks too.


回答1:


You didn't write to the Realm in the transaction. Try to add realm.copyToRealm(location) before realm.commitTransaction().



来源:https://stackoverflow.com/questions/36253617/issue-with-realm-unit-test-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!