Pergunta

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!

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top