I have this method
fun delete(id: Long) {
NotFoundExceptionValidator(!dishOfTheDayEntityRepository.existsById(id), "dishOfTheDay not found")
dishOfTheDayEntityRepository.deleteById(id)
}
NotFoundExceptionValidator this just checks if it's null then throws error
this is what I tried
@ConcurrentExecution
internal class DishOfTheDayServiceTest {
private val repo: DishOfTheDayEntityRepository = mockk()
private val mapper: DishOfTheDayMapper = mockk()
private val dishOfTheDayEntityService = DishOfTheDayService(repo, mapper)
@Test
fun `delete should work properly`() {
//given
val id: Long = 1;
//when
dishOfTheDayEntityService.delete(1)
//then
verify(exactly = 1) { repo.deleteById(1) }
}
}
when i run it it throws this error
no answer found for: DishOfTheDayEntityRepository(#1).existsById(1) io.mockk.MockKException: no answer found for: DishOfTheDayEntityRepository(#1).existsById(1)
CodePudding user response:
You forgot to mock your mocks behaviour, i.e. you should explicitly specify what the existsById()
and deleteById()
methods return. For example for existsById()
it should look like:
every { repo.existsById(id) } returns true
I suppose that the deleteById()
method returns Unit
so if you don't want to do it like above you can mock DishOfTheDayEntityRepository
like:
private val repo: DishOfTheDayEntityRepository = mockk(relaxUnitFun = true)
Now you don't have to mock Unit
returning methods of DishOfTheDayEntityRepository
. You can find more about it here.