Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Performs "verify no more interactions" check automatically for all mock objects (works with Mockito version 2 or higher & JUnit version 4 and 5). For detailed description: https://ufukuzun.wordpress.com/2019/04/09/ne-olup-bittiginden-habersiz-testlere-derman-mockscollector/ (Turkish)
import org.apache.commons.lang3.ArrayUtils;
import org.junit.After;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(MockitoJUnitRunner.class)
public abstract class BaseMockito2JUnit4Test {
private final Mockito2MocksCollector mockito2MocksCollector = new Mockito2MocksCollector();
@After
public void after() {
Object[] allMocks = mockito2MocksCollector.getAllMocks();
if (ArrayUtils.isNotEmpty(allMocks)) {
verifyNoMoreInteractions(allMocks);
}
mockito2MocksCollector.close();
}
protected InOrder inOrderVerifier() {
return inOrder(mockito2MocksCollector.getAllMocks());
}
}
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import java.util.stream.Stream;
import static org.mockito.Mockito.inOrder;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class BaseMockito2JUnit5Test {
private final Mockito2MocksCollector mockito2MocksCollector = new Mockito2MocksCollector();
@AfterEach
void after() {
Object[] allMocks = mockito2MocksCollector.getAllMocks();
if (ArrayUtils.isNotEmpty(allMocks)) {
Stream.of(allMocks).forEach(Mockito::verifyNoMoreInteractions);
}
mockito2MocksCollector.close();
}
protected InOrder inOrderVerifier() {
return inOrder(mockito2MocksCollector.getAllMocks());
}
}
import org.mockito.Mockito;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import org.mockito.listeners.MockCreationListener;
import java.util.LinkedList;
import java.util.List;
public final class Mockito2MocksCollector {
private final List<Object> createdMocks;
private final MockCreationListener mockCreationListener;
public Mockito2MocksCollector() {
createdMocks = new LinkedList<>();
MockingProgress mockingProgress = ThreadSafeMockingProgress.mockingProgress();
mockCreationListener = (mock, settings) -> createdMocks.add(mock);
mockingProgress.addListener(mockCreationListener);
}
public void close() {
Mockito.framework().removeListener(mockCreationListener);
}
public Object[] getAllMocks() {
return createdMocks.toArray();
}
}
@yusufcakal

This comment has been minimized.

Copy link

@yusufcakal yusufcakal commented Oct 21, 2019

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment