Issue
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.
Solution
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.
Answered By - VergiliQ
Answer Checked By - Willingham (JavaFixing Volunteer)