question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Realm cannot be configured when all test classes are run in an Android project

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
nhachichacommented, Jun 5, 2017

@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

0reactions
beeendercommented, Jun 26, 2017

No, tests should be run sequentially by default. A ParallelRunner was introduced recently but i guess you are not using it.

From our experience, that normally happened when you start some thread and didn’t wait for termination before teardown called.

Anyway, good to know the issue has been solved!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot run Robolectric test in Android Studio using Realm + ...
I was using Realm, PowerMockito and Robolectric to write test for my app. The test is running fine if I use ./gradlew test...
Read more >
Realm: Create reactive mobile apps in a fraction of the time
Step 2: Apply the realm-android plugin to the top of the application level ... With an inMemory configuration, you can create a Realm...
Read more >
Advanced test setup - Android Developers
Test in Android Studio and Test from the command line explain how to set up and run basic test configurations. However, when your...
Read more >
Create reactive mobile apps in a fraction of the time - Realm
You just Import Project in Android Studio and hit run . The RealmIntroExample in the root folder contains simple examples of how you...
Read more >
R class cannot be found in test project [37020767]
In Android Studio, everything looks fine. But when I try to run my tests, I get an error message saying that the class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found