This cannot be achieved because threaded module you created is running on a thread separate from JUnit test thread. The try catch block can only catch throwable inside its own thread.
How to catch java assertion error during runtime
-
02-07-2022 - |
Domanda
Pre-condition: VM param -ea is enabled
Example in source code:
assert booleanVariable;
which will throw AssertionError if the booleanVariable is false.
I wrote JUnit tests which results in FALSE booleanVariable, so how to catch the error instead of halted JUnit tests without using plugins. Note that the application functions as a threaded module. Therefore, JUnit tests calls a generic postMessage method like below:
@Test
public void myTest(){
Message invalidMessage = new Message("I am invalid");
//somewhere in threadedModule source code would throw AssertionError
//after reading from its inputQueue
threadedModule.postMessage(invalidMessage );
assertNotNull(onputQueue.waitForNextMessage(timeOutTime,timeOutMessage));
}
Thank you!
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow