1

I am writing JUnit test cases. I have a class that contains APIs which call JNI APIs.

public class Test {

    public native int getId_native();

    public void doSomething() {
        // do init
        int id = getId_native();
        // Get name by Id
    }

}

And I am writing unit test for doSomething() API. The problem that I am facing is, as soon as the JNI API(getId_native()) is called, UnsatisfiedLinkError is thrown and caught in my JUnit test and the test ends. Control does not proceed further to test the lines of code after the native function. This reduced the code coverage in all similar methods in the project.

Since the native methods are declared in the same class, I am unable to mock the class too.

Can someone help me how to overcome this issue?

Thanks.

Thirumal
  • 8,280
  • 11
  • 53
  • 103
Androider
  • 23
  • 4
  • You can move the native call out to a separate class which provides the lookup/native functionality on its own to facilitate testing, or try using powermock and simply have the native calls do nothing (and suppress any static blocks if needed) – Gryphon Aug 31 '20 at 05:44

1 Answers1

1

You need to have native library loaded during your tests. You can load dll with various ways. For example:

System.load("path to your dll with native method");

If original lib is not suitable for testing then build mock native dll with functionality expected in your unit tests.

VergiliQ
  • 26
  • 1