Question

I am searching for tools or solutions for unit testing JNI source code. So far my search cart hasn't found any solution for unit testing JNI code.

The option that I have is to write the unit test in java and make the native calls, verify the return value. The problem here is that I won't be able to get a code coverage report. And the calls to the native implementation makes, will need to be mocked or supported by a native stubbed implementation.

java test code (calls) ---> Native implementation in DLL (JNI) --- > Mocked/Stubbed C++ code

Please share, if there are any solutions available.

Was it helpful?

Solution

There really aren't any comprehensive tools available to automatically build and test JNI code. What I try and do is create a stand alone native library that I can unit test with something like MinUnit from a build system like Automake. My actual JNI dependent code is then compiled after my Java code from Ant using some custom tasks I created called JNITasks and then test via JUnit in the same way you would test any other Java class.

I don't think its going to be possible to get a native code coverage report for the native code via JUnit. I try to keep the JNI layer as thin as possible by only handling what is required to translate the Java input to something the native library can understand and vice versa. This should keep your JUnit testing fairly simple for native methods. All your complex native testing should happen after compiling the backing library, independently of JUnit. You maybe able to get a code coverage report from the native testing framework you decide to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top