realm-java: Realm cannot be configured when all test classes are run in an Android project

Goal

Run all tests in an Android project.

Expected Results

All tests run and pass.

Actual Results

When the tests in a class are run independently, they pass. When the full suite is launched, Realm cannot be configured. The exception:

06-05 13:38:20.764 21665-21692/com.example.sampleapp I/TestRunner: io.realm.exceptions.RealmFileException: No such file or directory () (/data/data/com.example.sampleapp/files/7db689c6-b5e7-4f59-a5f0-33a2701b4120.realm) in /home/cc/repo/realm/release/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 217 Kind: ACCESS_ERROR.
                                                                     at io.realm.internal.SharedRealm.nativeGetSharedRealm(Native Method)
                                                                     at io.realm.internal.SharedRealm.<init>(SharedRealm.java:190)
                                                                     at io.realm.internal.SharedRealm.getInstance(SharedRealm.java:237)
                                                                     at io.realm.BaseRealm.<init>(BaseRealm.java:80)
                                                                     at io.realm.Realm.<init>(Realm.java:141)
                                                                     at io.realm.Realm.createAndValidate(Realm.java:342)
                                                                     at io.realm.Realm.createInstance(Realm.java:321)
                                                                     at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:145)
                                                                     at io.realm.Realm.getInstance(Realm.java:284)
                                                                     at com.example.sampleapp.port.adapter.query.model.realm.RealmPassQueryRepositoryTest.setUp(RealmPassQueryRepositoryTest.java:41)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
                                                                     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
                                                                     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
                                                                     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
                                                                     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
                                                                     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
                                                                     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
                                                                     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
                                                                     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                                     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                                     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                                     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                                     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                     at org.junit.runners.Suite.runChild(Suite.java:128)
                                                                     at org.junit.runners.Suite.runChild(Suite.java:27)
                                                                     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                                     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                                     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                                     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                                     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                                                                     at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                                                                     at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
                                                                     at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
                                                                     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)

Steps & Code to Reproduce

Run all tests in project: failure. Run test in a class: pass.

Code Sample

    @Before
    public void setUp()  {
        Context context = InstrumentationRegistry.getTargetContext();
        Realm.init(context);
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
                .name(UUID.randomUUID().toString() + ".realm")
                .inMemory()
                .deleteRealmIfMigrationNeeded()
                .build();
        realm = Realm.getInstance(realmConfiguration);
        repository = new RealmPassQueryRepository(realm);
        realm.beginTransaction();
    }

    @After
    public synchronized void tearDown() {
        realm.commitTransaction();
        realm.close();
    }

(I create a random realm in order to avoid further problems during testing)

Version of Realm and tooling

Realm version(s): 3.1.4

Realm sync feature enabled: yes/no. No idea. All defaults.

Android Studio version: 2.3.2

Which Android version and device: Emulator. Pixel with Nougat 7.1.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 22 (11 by maintainers)

Most upvoted comments

@Villason 1- Why using deleteRealmIfMigrationNeeded since you generate a unique Realm for each test using UUID 2- I would avoid opening a transaction in the setup it’s part of each test logic. 3- Using inMemory will delete (on disk) the Realm file if you close all instances of the Realm

Having said that, there’s nothing funny with the configuration you build on the setup. we need to see the full test suite (ex: what’s happening inside RealmPassQueryRepository)

You can share your code (privately if you want) at help@realm.io