title: MockK no answer found for:
body: <p>I'm trying to test a method in my Service Implementation with JUnit and MockK library.</p>
  <p>PlanServiceFeatureImplTest.kt</p>
  <pre class="lang-kotlin prettyprint-override"><code>@Test
    fun `storeInstallmentPlan`(@MockK user: User) {
      val debtId = 123L
      val amount = BigDecimal.valueOf(1000)
      val interval = Repeat.monthly
  
      val firstPaymentDate = LocalDate.of(
        2021, 11, 4
      ).atStartOfDay(Time.DEFAULT_TIME_ZONE).toOffsetDateTime()
  
      val planDTO = InstallmentPlanDTO(
        interval = interval,
        firstPaymentDate = firstPaymentDate,
        amount = amount,
        installments = listOf()
      )
      val debt = Debt(
        userId = 32L,
        title = &quot;debt1&quot;,
        amount = amount,
        category = DebtCategory.credit
      )
      val plan = InstallmentPlan(
        id = 122L,
        debtId = debtId,
        interval = interval,
        firstPaymentDate = firstPaymentDate,
        amount = amount
      )
      val installment1 = Installment(
        id = 34,
        debtId = debtId,
        recordId = 13
      )
      val installment2 = Installment(
        id = 35,
        debtId = debtId,
        recordId = 14
      )
  
  
      val newStartDate = ZonedDateTime.parse(&quot;2021-10-05T00:00+02:00[Europe/Berlin]&quot;)
      val newEndDate = ZonedDateTime.parse(&quot;2021-11-04T00:00+01:00[Europe/Berlin]&quot;)
      val installments = listOf(
        WalletRecord(
          userId = debt.userId,
          type = RecordType.debt_rate,
          amount = plan.amount,
          title = debt.title,
          category = RecordCategory.debt_rate,
          repeat = plan.interval,
          startDate = newStartDate.toOffsetDateTime(),
          endDate = newEndDate.toOffsetDateTime(),
        )
      )
  
      val records = flowOf(
        WalletRecord(
          id = 43,
          userId = debt.userId,
          type = RecordType.debt_rate,
          amount = plan.amount,
          title = debt.title,
          category = RecordCategory.debt_rate,
          repeat = plan.interval,
          startDate = newStartDate.toOffsetDateTime(),
          endDate = newEndDate.toOffsetDateTime(),
        )
      )
  
  
      every { user.tz } returns &quot;Europe/Berlin&quot;
      coEvery { debtRepository.findById(debtId) } returns debt
      coEvery { installmentPlanRepository.findByDebtId(debtId) } returns plan
      coEvery {
        installmentPlanRepository.save(
          planDTO.copy(
            amount = amount
          ).toEntity(
            id = plan.id,
            debtId = debtId
          )
        )
      } returns InstallmentPlan(plan.id, debtId, interval, firstPaymentDate, amount )
      coEvery { installmentRepository.findByDebtId(debtId) } returns flowOf(installment1, installment2)
      coEvery {
        installmentRepository.deleteAllById(listOf(installment1.id!!, installment2.id!!).asIterable())
      } just Runs
      coEvery { recordService.deleteAll(listOf(installment1.recordId, installment2.recordId)) } just Runs
      coEvery { userService.findById(debt.userId) } returns user
      coEvery { recordService.saveAll(installments) } returns records
      coEvery {
        installmentRepository.saveAll(
          records.map {
            Installment(
              debtId = debtId,
              recordId = it.id!!
            )
          }
        ).map { it.recordId }
      } returns flowOf(43)
  
      runBlocking { planService.storeInstallmentPlan(debtId, planDTO) }
    }
  </code></pre>
  <p>PlanServiceFeatureImpl.kt</p>
  <pre class="lang-kotlin prettyprint-override"><code>@Transactional
    override suspend fun storeInstallmentPlan(debtId: Long, installmentPlanDTO: InstallmentPlanDTO): Flow&lt;Long&gt; {
      val debt = debtRepository.findById(debtId) ?: throw NotFoundException(&quot;Could not find debt with id $debtId&quot;)
      val installmentPlanId = installmentPlanRepository.findByDebtId(debtId)?.id
  
      val minimumAmount =
        BigDecimal.ONE.max(debt.amount.multiply(MINIMUM_INSTALLMENT_PERCENTAGE_OF_TOTAL_AMOUNT, MathContext.DECIMAL32))
          .setScale(2, RoundingMode.HALF_UP)
          .stripTrailingZeros()
      val maximumAmount = debt.amount
      val clampedAmount = maximumAmount.min(minimumAmount.max(installmentPlanDTO.amount))
  
      val installmentPlan = installmentPlanDTO.copy(
        amount = clampedAmount,
      ).toEntity(
        id = installmentPlanId,
        debtId = debtId,
      )
  
      installmentPlanRepository.save(installmentPlan)
      // delete existing records associated with the installment plan
      val existingInstallments = installmentRepository.findByDebtId(debtId).toList()
      installmentRepository.deleteAllById(existingInstallments.map { it.id!! })
      recordService.deleteAll(existingInstallments.map { it.recordId })
  
      // calculate installments / records
      /*
           This calculation follows this invariant:
            debt.amount = countOfFullInstallments * amount + lastInstallment
         */
      val user = userService.findById(debt.userId)
        ?: throw NotFoundException(&quot;Could not find user that owns debt $debtId&quot;)
      val zoneId = ZoneId.of(user.tz)
      val firstPaymentDate = installmentPlan.firstPaymentDate.atZoneSameInstant(zoneId)
  
      val countOfFullInstallments =
        debt.amount.divide(
          (if (installmentPlan.amount &lt;= BigDecimal.ONE) BigDecimal.ONE else installmentPlan.amount),
          MathContext.DECIMAL32
        )
          .setScale(0, RoundingMode.DOWN)
          .intValueExact()
      val lastInstallmentAmount = debt.amount - installmentPlan.amount * countOfFullInstallments.toBigDecimal()
      val countOfInstallments = countOfFullInstallments + if (lastInstallmentAmount &gt; BigDecimal.ZERO) 1 else 0
  
      val installments = List(countOfInstallments) { i -&gt;
        val endDate =
          addInterval(firstPaymentDate, installmentPlan.interval, i)
        val startDate = addInterval(firstPaymentDate, installmentPlan.interval, i - 1)
          .plusDays(1)
        val recordAmount = if (i &lt; countOfFullInstallments)
          installmentPlan.amount
        else lastInstallmentAmount
        WalletRecord(
          userId = debt.userId,
          type = RecordType.debt_rate,
          amount = recordAmount,
          title = debt.title,
          category = RecordCategory.debt_rate,
          repeat = installmentPlan.interval,
          startDate = startDate.toOffsetDateTime(),
          endDate = endDate.toOffsetDateTime(),
        )
      }
  
      val recordsFlow = recordService.saveAll(installments)
      return installmentRepository.saveAll(recordsFlow.map {
        Installment(
          debtId = debtId,
          recordId = it.id!!,
        )
      }).map { it.recordId }
    }
  </code></pre>
  <p>I get this error:</p>
  <pre><code>no answer found for: InstallmentRepository(installmentRepository#4).saveAll(app.backend.plan.PlanServiceFeatureImpl$storeInstallmentPlan$suspendImpl$$inlined$map$1@31e1ec3)
  io.mockk.MockKException: no answer found for: InstallmentRepository(installmentRepository#4).saveAll(app.backend.plan.PlanServiceFeatureImpl$storeInstallmentPlan$suspendImpl$$inlined$map$1@31e1ec3)
  </code></pre>
  <p>It is a lot of code, but since I don't know where the error comes from I provide the full code. In other cases with the error 'no answer found for' it provided me something like '[...]InstallmentRepository(...).saveAll([parameters here])' and not a path to the Unit under test.
  Hope someone can help me with this.</p>