3

I'm working on fixing a JNI call that's crashing at the moment. Striving to be a good TDD practitioner, I've created a JUnit test to replicate the problem, which it does admirably.

However, by crashing, the JNI call never returns. Is it possible to write a JUnit test that reports a failure on a JNI crash?

Darcy Casselman
  • 2,574
  • 3
  • 24
  • 26

2 Answers2

4

Ouch, if your JNI is crashing the JVM, then that's certainly going to be difficult to test from junit. You can't, for example, hook up a signal handler for SIGSEGV, the JVM's just going to die.

If it were me, I would change this around a little bit and create a simple Java class with a Main that calls the crashing native code, and in my junit test I would execute a new instance of a Java runtime that runs that wrapper class using Runtime#exec.

You can then wait for the resultant process to finish and check its return code - if it's non-zero, there was a problem in execution.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
2

try using the timeout parameter: http://junit.sourceforge.net/javadoc/org/junit/Test.html if you are using junit 4, otherwise something like: http://netbeans.org/kb/docs/java/junit-intro.html#Exercise_243

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90