Robolectric tanks on my Application object that loads a JNI library

1,981 views
Skip to first unread message

Christopher Perry

unread,
May 3, 2012, 1:30:09 AM5/3/12
to Robolectric
Here's the stack trace:

java.lang.UnsatisfiedLinkError: no cperryinc-jni in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1045)
at
com.cperryinc.application.MoolaApplication.<clinit>(MoolaApplication.java:
24)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at
com.xtremelabs.robolectric.internal.ClassNameResolver.safeClassForName(ClassNameResolver.java:
36)
at
com.xtremelabs.robolectric.internal.ClassNameResolver.resolve(ClassNameResolver.java:
15)
at
com.xtremelabs.robolectric.ApplicationResolver.newApplicationInstance(ApplicationResolver.java:
71)
at
com.xtremelabs.robolectric.ApplicationResolver.resolveApplication(ApplicationResolver.java:
28)
at
com.xtremelabs.robolectric.RobolectricTestRunner.createApplication(RobolectricTestRunner.java:
483)
at
com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:
360)
at
com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:
299)
at
com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:
277)
at
org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:
79)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
71)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:
76)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:
182)
at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:
62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:
120)

Jan Berkel

unread,
May 3, 2012, 6:18:57 AM5/3/12
to robol...@googlegroups.com


i use the following to test JNI code:

class MyJniClass {
 static {
        try {
            System.loadLibrary("libname");
        } catch (UnsatisfiedLinkError e) {
            // only ignore exception in non-android env
            if ("Dalvik".equals(System.getProperty("java.vm.name"))) throw e;
        }
    }
}

then in the testrunner:

public class MyTestRunner  extends RobolectricTestRunner {
   public MyTestRunner(Class testClass) throws InitializationError {
         // remove native calls + replace with shadows
        addClassOrPackageToInstrument("com.example.jni.MyJniClass");
   }

   protected void bindShadowClasses() {
         // bind shadow JNI classes
   }
}




Fla Lau

unread,
Jul 16, 2013, 6:44:24 AM7/16/13
to robol...@googlegroups.com
Sorry to resurrect this topic but this solution only works for 1.2.

Do you have a workaround for the 2.+ ?

Anmin Gong

unread,
Oct 31, 2013, 11:42:56 PM10/31/13
to robol...@googlegroups.com
Hi Flavien,
  Do you have solution now?
  I'm facing the same problem.

在 2013年7月16日星期二UTC+8下午6时44分24秒,Flavien Laurent写道:

Cain Wong

unread,
May 22, 2014, 6:47:48 PM5/22/14
to robol...@googlegroups.com
Hello all. Has anyone posted a solution to this problem for 2.+ ?

Guillaume Bero

unread,
Jun 16, 2014, 10:55:11 AM6/16/14
to robol...@googlegroups.com
Did you manage to get it working ?

AmArAiAr Pathak

unread,
Jul 9, 2014, 10:32:48 AM7/9/14
to robol...@googlegroups.com
 I am facing the same issue , is there a way to resolve this ? Could not find anything for 2.x on other groups and discussions as well.

Chandra

unread,
Mar 5, 2015, 1:59:44 AM3/5/15
to robol...@googlegroups.com
Has anyone found a solution to this - I am stuck on this too.

Cain Wong

unread,
Mar 5, 2015, 1:20:57 PM3/5/15
to robol...@googlegroups.com
I ultimately got around this by abstracting the JNI behind an interface, and using mocks so that JNI is not needed for my tests at all.
Reply all
Reply to author
Forward
0 new messages