使用JUnit4 Test Runner,测试运行并且似乎有效,但查询没有返回结果:
@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));
}
是的,我查看了示例项目,但不想添加所有依赖项,并希望我的测试可读,所以试图避免所有的模拟。
答案 0 :(得分:1)
您在交易中没有写入Realm。
尝试在realm.copyToRealm(location)
之前添加realm.commitTransaction()
。