0

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.

3
  • 1
    You didn't write to the Realm in the transaction. Try to add realm.copyToRealm(location) before realm.commitTransaction(). Commented Mar 28, 2016 at 2:24
  • Why didn't you put this in as an answer? I used Realm in Swift, just moved to android and did not see those calls. I saw that method but thought it was for resyncing with the session as it were. Thanks! Commented Mar 28, 2016 at 4:45
  • Yeah, add the answer. cheers! Commented Mar 29, 2016 at 2:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.