18

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

jc12
  • 1,411
  • 1
  • 18
  • 24
  • How about configuring your test framework to instantiate test object on every test method run? In JUnit 5 it's a default setup. In Kotest [set `isolationMode` to `IsolationMode.InstancePerTest`](https://kotest.io/docs/framework/isolation-mode.html#instancepertest) – Ilya Serbis May 11 '23 at 14:03

2 Answers2

32

For resetting specific mocks in MockK, you can use clearMocks() method. Pass the mocks you would like to reset to it.

To create relaxed mock via annotation just check @RelaxedMockK.

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
oleksiyp
  • 2,659
  • 19
  • 15
9

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

Peter Westlin
  • 121
  • 2
  • 6