Why is this an issue?
JUnit assertions should not be made from the run
method of a Runnable
, because their failure may not be detected in the
test that initiated them. Failed assertions throw assertion errors. However, if the error is thrown from another thread than the one that initiated
the test, the thread will exit but the test will not fail.
How to fix it
Assertions in Runnable
tasks should be extracted or executed by the main thread to make the whole test fail.
Code examples
Noncompliant code example
class RunnableWithAnAssertion extends Thread {
@Override
public void run() {
Assert.assertEquals(expected, actual); // Noncompliant
}
@Test
void test() {
RunnableWithAnAssertion otherThread = new RunnableWithAnAssertion();
otherThread.start(); // The assertion in the run method above will be executed by other thread than the current one
// ...
// Perform some actions that do not make the test fail
// ...
Assert.assertTrue(true);
}
}
Compliant solution
class RunnableWithAnAssertion extends Thread {
@Override
public void run() {
Assert.assertEquals(expected, actual); // Noncompliant
}
@Test
void test() {
RunnableWithAnAssertion otherThread = new RunnableWithAnAssertion();
otherThread.run();
// ...
// The failed assertions in the run method will prevent us from reaching the assertion below
// ...
Assert.assertTrue(true);
}
}