You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Mockito verify with a timeout to wait for an asynchronous method call, it sometimes fails if the method is synchronized.
I have observed the effect with multiple versions of Eclipse, including 2020-06 (4.16.0), and both Mockito 3.1.0 and 3.4.6. The example below illustrates the problem (both the test and the subject under test are in the same class for simplicity).
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class A {
@Mock
private A a;
public synchronized void f() {
// Do stuff
}
@Test
void test() {
// Call a.f() asynchronously and with a small delay
// (to make the test thread yield).
new Thread(() -> {
try {
Thread.sleep(100);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
a.f();
}).start();
// This verification fails, but it should pass.
Mockito.verify(a, Mockito.timeout(2000)).f();
}
}
Noteworthy points:
The effect is not perfectly reproducible. In fact, my real-life examples never fail on some computers while they always fail on others, suggesting a race condition.
Removing the synchronized keyword solves the issue, even when replacing it with a synchronized(this) block in the method body.
If the class containing the synchronized method implements an interface, mocking the interface instead of the concrete class solves the issue.
Removing the sleep in the example above, hence increasing the chance that the call to f is finished before verify is called, solves the issue.
Just to note that I'm seeing this bug by default in Mockito 5.0.0 - in 4.11.0 the provided test case passes but it fails in 5.0.0. I'm not able to get it to pass even without the sleep / with a longer timeout, on multiple machines. Only moving the synchronization to the body of the method works around the issue
When using Mockito verify with a timeout to wait for an asynchronous method call, it sometimes fails if the method is synchronized.
I have observed the effect with multiple versions of Eclipse, including 2020-06 (4.16.0), and both Mockito 3.1.0 and 3.4.6. The example below illustrates the problem (both the test and the subject under test are in the same class for simplicity).
Noteworthy points:
The text was updated successfully, but these errors were encountered: