Reputation: 3308
I am using Junit 4.x to test a method.
The method under the test is expected to throw an IllegalArgumentException. The method is a blocking method (synchronous) which internally makes a remote ssh connection executes a script and captures result, or throws exception.
To test this, I have written my JUnit test code as below.
@Test (timeout=3000, expected=IllegalArgumentException.class)
public final void testReadFolderWithInvalidFolder() {
final String folder = "/home/rv_nath/xxxyyyZzz";
rfc.readFolder(folder);
}
The testReadFolder() method above is supposed to wait upto 3 seconds (becoz of timeout=3000) before checking whether the expected exception is thrown or not). But it returns immediately, reporting that the test case has failed.
Can anyone help me figure out what is going wrong in the above. If more details required, let me know.
Here is the failure trace from Junit...
java.lang.AssertionError: Should have thrown IllegalArgumentException. at org.junit.Assert.fail(Assert.java:91) at com.comviva.remotefilebrowser.server.RemoteFileCaseTest.testReadFolderWithInvalidFolder(RemoteFileCaseTest.java:94) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.FailOnTimeout$1.run(FailOnTimeout.java:28)
Is there a bug in Junit4 timeout implementation, or am I missing something here?
Because, if I run the test case in debug mode (and trace through it, then it takes time to finish the remote execution, but it definitely works and the test case passes). The problem occurs only if I choose to run the test case, instead of debug it.
thanks and regards, RV
Upvotes: 1
Views: 861
Reputation: 3308
Sorry for the above naive question. It was a problem with the way, I used JSch library routines for reading remote data.
Just posting here, so it may help novice users like me :)
The JSch library was throwing some exception, which I was not capturing properly. The called method was capturing the exception and was returning immediately. After the correction, it works correctly.
Upvotes: 1