Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to reset all used mocks #513

Open
ajburley opened this issue Mar 15, 2024 · 0 comments
Open

Ability to reset all used mocks #513

ajburley opened this issue Mar 15, 2024 · 0 comments

Comments

@ajburley
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant