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.

Setup inMemory Realm for Instrumented Tests

See original GitHub issue

Goal

Setup inMemory Realm while running Instrumented tests

Actual Results

Realm cannot be initialized from androidTest package

Steps & Code to Reproduce

@Before
fun setup() {
    Realm.init(MessengerApplication.context)
    val config = RealmConfiguration.Builder()
            .name("realm.test")
            .inMemory()
            .build()
    realm = Realm.getInstance(config)
}

java.lang.NoSuchMethodError: No static method init(Landroid/content/Context;)V in class Lio/realm/Realm; or its super classes

Strangely, the code works if I setup realm in my Application class and set the default config to inMemory.

Version of Realm and tooling

Realm version(s): 4.1.1

Realm sync feature enabled: No

Android Studio version: 3.0

Which Android version and device: 7.0

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
cmelchiorcommented, Nov 3, 2017

Interesting. Thanks for the info 👍

0reactions
SarthakM9commented, Nov 3, 2017

@cmelchior I think I found the problem!

debug {
    minifyEnabled true
    useProguard false 
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

as I was removing all unused methods in the debug build, inMemory() was not found when I mentioned in the androidTest package. Moreover, when I moved all the Realm initialization to androidTest, it could not find anything! simply ignoring Realm classes in Proguard settings resolved the issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android - Realm - Instrumented Unit Tests - Stack Overflow
everybody. I have a question bounded to Realm. Realm is new technology for me. I need test functionality - correctness of updates, inserts, ......
Read more >
Android: Testing Realm - Medium
This article demonstrated an alternative approach to test Realm on Android. It relies on Realm's in-memory feature and Android's connected ...
Read more >
Testing - Java SDK — Realm - MongoDB
You can run integration tests by communicating with actual instances of Realm Database or an App backend using Android's built-in instrumented tests.
Read more >
Testing Room Database in Android using JUnit - GeeksforGeeks
Create a new class “Language.kt” and annotate it with @Entity and pass the table name. Kotlin. Kotlin ...
Read more >
Testing Realm Apps - Realm Academy
Note: the test above is far from being comprehensive, the point we're trying to make is on the setup. Benchmark: on-disk vs in-memory....
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