title: Kotlin: how to clean up or reset mocks with JUnit5 and Mockk
body: <p>In some case it's needed to clean up or reset the mocks between tests cases.  </p>
  
  <p>Using Kotling with JUnit5 and Mockk, a first approach should be like this:</p>
  
  <pre><code>class CreateProductsTests {
  
      @Test
      fun `run() with an existing product should throw a CrudException`() {
  
          val productRepository = mockk&lt;ProductRepository&gt;()
          val editorService = mockk&lt;EditorService&gt;()
          val sut = CreateProductServiceImpl(productRepository, editorService)
  
          // Given an editor that return a JSON
          val product = completeTestProduct()
          every { editorService.edit("Create new Product", product) } returns product
  
          // And the product does exist in the database
          every { productRepository.findById(product.id) } returns Optional.of(product)
  
          // When we call createProduct()"
          // Then should fail
          val exception = assertFailsWith&lt;CrudException&gt; { sut.createProduct() }
          exception.message shouldBe "The product 'TST' already exists in database"
      }
  
      @Test
      fun `createProduct() with an invalid product should fail`() {
  
          val productRepository = mockk&lt;ProductRepository&gt;()
          val editorService = mockk&lt;EditorService&gt;()
          val sut = CreateProductServiceImpl(productRepository, editorService)
  
          // Given an editor that return a JSON
          val product = completeTestProduct()
          every { editorService.edit("Create new Product", product) } returns product
  
          // And the product does exist in the database
          every { productRepository.findById(product.id) } returns Optional.of(product)
  
          // And a repository saves the product
          every { productRepository.save(product) } returns product
  
          // When we call createProduct()"
          val actual = sut.createProduct()
  
          // Then it should return the product
          actual shouldBe product
  
          // And should call once these dependencies
          verify(exactly = 1) {
              editorService.edit(any&lt;String&gt;(), any&lt;Product&gt;())
              productRepository.findById(any&lt;String&gt;())
              productRepository.save(any&lt;Product&gt;())
          }
      }
  }
  </code></pre>
  
  <p>But instead of declaring the mocks on every test case, and initializing the SUT, maybe it's clearer (maybe not faster) to use <code>@BeforeEach</code>, with something like this:</p>
  
  <pre><code>class CreateProductsTests {
  
      var productRepository = mockk&lt;ProductRepository&gt;()
      var editorService = mockk&lt;EditorService&gt;()
      var sut = CreateProductServiceImpl(productRepository, editorService)
  
      @BeforeEach
      fun clear() {
          productRepository = mockk&lt;ProductRepository&gt;()
          editorService = mockk&lt;EditorService&gt;()
          sut = CreateProductServiceImpl(productRepository, editorService)
      }
  ...
  </code></pre>
  
  <p><strong>Is there any better (and faster) way to declare the mocks and sut once and reset or clear all of them on every tests?</strong></p>