'MockK - reinitialize mocks for each test

I have some mocks created using: val someService = mockk<SomeService>(relaxed = true)

There are multiple tests in the file and I want the mock to be reset for each test

Is there currently a way to do this in MockK?

I know there is MockKAnnotations.init(this), but it didn't look like there was a way to set relaxed = true in the @Mock annotation



Solution 1:[1]

For resetting mocks in MockK you can use clearMocks. To create relaxed mock via annotation just check @RelaxedMockK

Solution 2:[2]

clearAllMocks() clears all mocks without the need of specifying them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 oleksiyp
Solution 2 Peter Westlin