Robolectric: Avoid loading the native library when running the unit test

Created on 10 Mar 2016  路  5Comments  路  Source: robolectric/robolectric

Hi,

I am testing a class which loads a jni library.

Something like this

public class myClass {
    static {
        System.loadLibrary("myjni-lib")
    }
    protected native TreeMap<Integer, byte[]> postData(byte[] byteData, String type);
    public boolean upload() {
    // do something
    }
}

I do not want to test jni library, but it still loads the library and throws

java.lang.UnsatisfiedLinkError: no myjni-lib in java.library.path

How can I avoid Robolectric to load the jni library?

It seems that mocking the System class is a bad idea, because System class may be used with Robolectric?

Thank you

Most helpful comment

@twmht Use a try/catch for now:

    static {
        try {
            System.loadLibrary(""myjni-lib");
        } catch (final UnsatisfiedLinkError e) {
            Log.e(TAG, "loadLibrary" + Log.getStackTraceString(e));
        }
    }

All 5 comments

@twmht Use a try/catch for now:

    static {
        try {
            System.loadLibrary(""myjni-lib");
        } catch (final UnsatisfiedLinkError e) {
            Log.e(TAG, "loadLibrary" + Log.getStackTraceString(e));
        }
    }

@jaredsburrows
Good trick! Adding a try/catch will solve most of the test failures.

See Issue #1171

Closing this as it hasn't been updated in a while. If its still an issue with Robolectric 4.0 please reopen with a reproducible test case and we'll prioritize.

cool

Was this page helpful?
0 / 5 - 0 ratings