vendredi 26 juin 2020

Testing Spring Controllers with Mockk and TestRestTemplate

I'm trying to test a controller with a mocked service.

private val serviceMockk = mockk<FooService>()
val fooDto = createFooDto(1)
 ...

every {serviceMockk.getById(fooDto.id) } returns fooDto

val result = restTemplate.exchange("http://localhost:$port/foo"+fooDto.id, HttpMethod.GET, null, 
String::class.java)

verify {serviceMockk.getById(fooDto.id)}

assertEquals(HttpStatus.OK, response.statusCode)

I get verify error, "serviceMockk.getById(fooDto.id) wasn't called".
RestTemplate.exchange returns "entity with id 1 not found".

Looks like restTemplate doesn't use mock service. What I did wrong?

Aucun commentaire:

Enregistrer un commentaire