Testing Realm with Robolectric

1,513 views
Skip to first unread message

Daniel Vogel

unread,
Nov 9, 2014, 2:10:45 PM11/9/14
to realm...@googlegroups.com
Hey, guys!

I'm using Robolectric (http://robolectric.org/) to test my Android app. The code to get an instance of Realm results in an UnsatisfiedLinkError.

Get an instance of Realm:
Realm.getInstance(Robolectric.application.getApplicationContext())

Result:
java.lang.UnsatisfiedLinkError: no tightdb-jni in java.library.path
   at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
   at java.lang.Runtime.loadLibrary0(Runtime.java:849)
   at java.lang.System.loadLibrary(System.java:1088)
   at io.realm.internal.TightDB.loadLibrary(TightDB.java:144)
   at io.realm.internal.SharedGroup.<clinit>(SharedGroup.java:32)
   at io.realm.Realm.<init>(Realm.java:108)
   at io.realm.Realm.createAndValidate(Realm.java:366)
   at io.realm.Realm.create(Realm.java:355)
   at io.realm.Realm.getInstance(Realm.java:314)
   at io.realm.Realm.getInstance(Realm.java:193)

I was wondering if anyone has successfully used Robolectric to test their Realm app.

Thanks!

Kenneth Geisshirt

unread,
Nov 10, 2014, 4:30:26 AM11/10/14
to Daniel Vogel, realm...@googlegroups.com
Hey Daniel,

First, thanks for trying out Realm. We really appreciate your feedback.

> I'm using Robolectric (http://robolectric.org/) to test my Android app. The code to get an instance of Realm results in an UnsatisfiedLinkError.

It sounds like that the shared objects (the .so files) weren’t copied into your build environment. Are you using Eclipse? There is a few manuel steps involved when using Eclipse - see the “Installation” section in our documentation: http://realm.io/docs/java/0.73.0/ (maybe you forgot step 2 in the Eclipse guide).

/kneth

--
Kenneth Geisshirt, M.Sc., Ph.D. · +45 60 62 71 82 · k...@realm.io
Njalsgade 19D, DK-2300 Copenhagen S · http://realm.io/

Daniel Vogel

unread,
Nov 10, 2014, 8:39:43 AM11/10/14
to realm...@googlegroups.com, voge...@gmail.com
Thanks, Kenneth! 

I'm using IntelliJ (basically, Android Studio) as my IDE. 

Would I have to do step 2 of the Eclipse guide if using IntelliJ? 

Kenneth Geisshirt

unread,
Nov 10, 2014, 8:50:27 AM11/10/14
to Daniel Vogel, realm...@googlegroups.com
> On 10 Nov 2014, at 14:39, Daniel Vogel <voge...@gmail.com> wrote:
>
> I'm using IntelliJ (basically, Android Studio) as my IDE.
>
> Would I have to do step 2 of the Eclipse guide if using IntelliJ?

If you’re using Gradle, you should be able to point to Realm in your build file. If you copied the jar file manually, then you should copy the .so files too.

Daniel Vogel

unread,
Nov 10, 2014, 8:57:05 AM11/10/14
to realm...@googlegroups.com, voge...@gmail.com
I'm using Gradle.

I am able to successfully use Realm when running my app on the device/emulator, but not when running my unit tests with Robolectric. http://robolectric.org/

I was wondering if anyone has been able to use Robolectric (or another Android unit testing framework) to test an app that uses Realm.

Thanks!

Kenneth Geisshirt

unread,
Nov 10, 2014, 9:34:09 AM11/10/14
to Daniel Vogel, realm...@googlegroups.com

> On 10 Nov 2014, at 14:57, Daniel Vogel <voge...@gmail.com> wrote:

> I am able to successfully use Realm when running my app on the device/emulator, but not when running my unit tests with Robolectric. http://robolectric.org/

Is it possible for you to share a small (non-working) app? It could help the debugging.

Daniel Vogel

unread,
Nov 10, 2014, 10:15:08 AM11/10/14
to realm...@googlegroups.com, voge...@gmail.com
Here is an example of getting an instance of Realm in a Robolectric test:

@RunWith(RobolectricTestRunner.class)
public class RealmTest {

  @Test
  public void realmShouldWork() {
    Realm realm = Realm.getInstance(Robolectric.application.getApplicationContext());
  }
}

The call to "getInstance" is where the originally posted exception occurs.

TimNew

unread,
Nov 13, 2014, 3:20:43 AM11/13/14
to realm...@googlegroups.com, voge...@gmail.com
Same issue here.
I assume to include Realm into Robolectric test, we need to create new shadow classes for Realm classes. At least, for all the facade classes.

iker martin

unread,
Jan 9, 2015, 3:06:34 PM1/9/15
to realm...@googlegroups.com
Same issue for me.

Kenneth Geisshirt

unread,
Jan 12, 2015, 7:14:15 AM1/12/15
to Iker Martin, realm...@googlegroups.com
At Realm, we are aware that the Robolectric + Realm combo has issues. I have opened an issue at Robolectric (https://github.com/robolectric/robolectric/issues/1389). Please consider comment on that issue to raise the awareness at Robolectric about supporting Realm.

Best regards,
Kenneth

--
Kenneth Geisshirt
Member of technical staff


--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/6008daf3-1d37-4186-b73c-60c823256f2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:64769119-287#}
Reply all
Reply to author
Forward
0 new messages