I have a project that creates both a jni .so file and a java .jar file. All this is working as expected except JUnit test execution. I need the JUnit test to run after the native-maven-plugin creates the .so file. JUnit test will always fail when new JNI methods are added because they are running before the shared library has been created. Is the only option to build twice? Meaning build once skipping test then either execute tests separately or run build again to execute the test?
How to get JUnit tests to run after JNI .so creation with native-maven plugin
151 Views Asked by musterjunk At
2
There are 2 best solutions below
0
On
Ok, this was a project structure issue. I was trying to get tests that belonged to a different project to run after my JNI .so was built in my JNI project. What I needed to do was put the JNI integration tests I was creating in src/java/test in the JNI project, not another project and try to order them somehow.
jni -
|
src
|
java
|
test
not
other project-
|
src
|
java
|
test
of course maven ran the other project test after the other project was built. That is what it suppose to do. Even though the JNI project built a C .so file. The Java rule for testing still apply to the JNI project when it comes to running java tests.
i guess you are using the native:link goal from the native-maven-plugin?
if you look at the documentation of the goal you see that it is bound by default to the lifecycle phase package.
junit tests (run by maven-surefire-plugin) are run by default in the lifecycle phase test (https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html).
in the maven lifecycle reference you can see that the test phase is before the package phase. this explains why your tests fail after you have made changes. Tests are run first, .so file is generated afterwards.
You can use the maven-failsafe-plugin to run tests after package phase.
add the plugin to your build and name all tests that should be run after package to MySpecificFeatureIT (the suffix IT stands for Integration Test). Tests named MyFeatureTest will still be run in test phase.
see the plugin documentation for more details