Hey Mockito team.
I have a question about verifying static methods calls. I am aware of Mockito.inOrder(Object... mocks), but we can't pass a MockedStatic instance. Can we verify that some static methods have been called in a specific order? Here is a minimal, reproducible example:
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
class StaticContext {
public static void first() { }
public static void second() { }
}
class ExampleTest {
@Test
public void shouldCallStaticMethodsInExpectedOrder() {
// GIVEN
try (MockedStatic<StaticContext> mockedStatic = Mockito.mockStatic(StaticContext.class)) {
// WHEN
StaticContext.first();
StaticContext.second();
// THEN
mockedStatic.verify(StaticContext::first);
mockedStatic.verify(StaticContext::second);
}
}
}
Dependencies:
org.junit.jupiter:junit-jupiter-engine:5.8.2
org.mockito:mockito-inline:4.2.0
How can we make the test fail if we swap StaticContext.first() with StaticContext.second()? I have tried also using verify with a lambda, i.e.:
mockedStatic.verify(() -> {
StaticContext.first();
StaticContext.second();
});
But, again, the order isn't enforced. I would expect to have a way to do it, since we can do it with regular mocks. Otherwise, it would be interesting to implement it.
Please, share your thoughts, thank you! 🙂