Currently, I'm working on unit tests and I'm using Mockk for that purpose.
Here's how my unit test looks like (I'll put the most important lines)
@Test
fun `Test computeDowntimesOverPeriod with starting uptime`() {
every {incidentRepository.incidentsBySiteKey(any(), any(), or(ofType(String::class), isNull()))} returns buildIncidentsStream(5)
mockSAT123Mapping()
val timeIntervals = incidentManager.computeDowntimesOverPeriod("SAT.123", Period.ofWeeks(1))
//...
verify(exactly=1) {incidentRepository.incidentsBySiteKey(Period.ofDays(7),15, "asc")}
}
Here's the signature of the incidentsBySiteKey function
fun incidentsBySiteKey(period: Period, siteId: Int?, order: String = "asc"): Stream<Incident>
For the computeDowntimesOverPeriod function
open fun computeDowntimesOverPeriod(siteKey: String, period: Period): List<StatusTimeInterval> {
//...
val downtimes = incidentsBySiteKeyStream(siteKey, period)
//...
}
The incidentsBySiteKeyStream function :
private fun incidentsBySiteKeyStream(siteKey: String, period: Period, order: String = "asc"): Optional<Stream<Incident>> {
Objects.requireNonNull(period)
return if (StringUtils.isEmpty(siteKey)) {
Optional.of(Stream.empty())
} else siteMapper.fromCMMSKey(siteKey)
.flatMap { it.toUtId() }
.map { siteId -> incidentRepository.incidentsBySiteKey(period, siteId, order).filter { equipmentRepository.findById(it.equipmentId).get().weight > 0 } }
}
As you can see, in the computeDowntimesOverPeriod function, we use the incidentsBySiteKey function. So, in the test it's logical to check whether or not the function is called. But when I run the test, I get the following error :
io.mockk.MockKException: no answer found for: IncidentRepository(incidentRepository#28).incidentsBySiteKey(P7D, 15, asc)
So basically, my test failed when I initialized the timeIntervals variable. But why ? I mean, I have the every block that should return something for any argument.
Edit : kotlin version => 1.3.31 mockk version => 1.8.13.kotlin13