mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.09k stars 198 forks source link

Ability to reset all used mocks #513

Open ajburley opened 3 months ago

ajburley commented 3 months ago

In Java code, when declaring mocks with annotations, it's possible to reset all mocks at once, like this:

@Mock
Foo mockFoo;

@Mock
Bar mockBar;

private AutoCloseable mockCloser;

@Before
public void setUp() {
    mockCloser = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() {
    mockCloser.close();
}

I can't see any equivalent way of doing this in mockito-kotlin when using the mock() helper. Which leads us to generally just call reset in our @After method.

val mockFoo: Foo = mock()
val mockBar: Bar = mock()

@After
fun tearDown() {
    reset(
        mockFoo,
        mockBar
    )
}

The problem is, we always get problems where people (usually me) add extra mocks and then forget to update the reset.

I am wondering if mockito-kotlin supports this use case, or could do so? If there is no existing solution, I was wondering if using Kotlin property delegates could help, as we could use them to register the mock with some object on assignment, and then call that object to reset all the mocks.