I have a spring restcontroller (written in kotlin and running spring boot 2.1.7, using spring-mockk as mock framework for testing) which takes a list of locales as parameter. In my test, I'm randomly generating a list of locale and verifying whether the controller is calling the service using the same list of locales. This test is failing 2/3 times out of every 10 times. I checked the logs and the list of locales are the exactly same. what am I doing wrong?
The code for my controller:
@RequestMapping("/api/v1/locale")
@RestController
class LocaleController (private val service: LocaleService){
@GetMapping
fun simpleApi(@RequestParam locales: List<Locale>) = service.getLocale(locales)
}
my dummy service:
@Service
class LocaleService {
fun getLocale(list: List<Locale>) = "1"
}
my test code:
@WebMvcTest(controllers = [LocaleController::class])
class LocaleControllerTest {
@Autowired
lateinit var mvc : MockMvc
@MockkBean
lateinit var service: LocaleService
@RepeatedTest(10)
fun `test locale api`(){
every { service.getLocale(any()) } returns "1"
val list = randomList{ locale() }
get("/api/v1/locale")
.param("locales", *list.map { it.toString() }.toTypedArray())
.let { mvc.perform(it) }
.andExpect (status().isOk)
verify {
service.getLocale(list)
}
}
private fun locale(): Locale {
val availableLocales = getAvailableLocales()
return availableLocales[(0..1000).random() % availableLocales.size]
}
private fun <T> randomList(f: (Int) -> T) = List(10, f)
}
This test is failing 2/3 times out of every 10 times any time I run. The test log:
java.lang.AssertionError: Verification failed: call 1 of 1: LocaleService(org.dripto.springkotlinplayground.LocaleService#0 bean#1).getLocale(eq([sr_BA, ja_JP_JP_#u-ca-japanese, et, es_SV, sq, ar, fr_CH, tr, ca_ES, ar_QA]))). Only one matching call to LocaleService(org.dripto.springkotlinplayground.LocaleService#0 bean#1)/getLocale(List) happened, but arguments are not matching:
[0]: argument: [sr_BA, ja_JP_JP_#u-ca-japanese, et, es_SV, sq, ar, fr_CH, tr, ca_ES, ar_QA], matcher: eq([sr_BA, ja_JP_JP_#u-ca-japanese, et, es_SV, sq, ar, fr_CH, tr, ca_ES, ar_QA]), result: -
Stack trace:
io.mockk.impl.InternalPlatform.captureStackTrace (InternalPlatform.kt:121)
io.mockk.impl.stub.MockKStub.handleInvocation (MockKStub.kt:247)
io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation
Aucun commentaire:
Enregistrer un commentaire