Skip to content

Instantly share code, notes, and snippets.

@mathias21
Last active July 31, 2020 11:08
Show Gist options
  • Save mathias21/9255c650744ed2ef1be0da876c7df8f0 to your computer and use it in GitHub Desktop.
Save mathias21/9255c650744ed2ef1be0da876c7df8f0 to your computer and use it in GitHub Desktop.
KtorEasy routing testing example 1
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class RegistrationRoutingTest : BaseRoutingTest() {
private val registrationController: RegistrationController = mockk()
@BeforeAll
fun setup() {
koinModules = module {
single { registrationController }
}
moduleList = {
install(Routing) {
registrationModule()
}
}
}
@BeforeEach
fun clearMocks() {
clearMocks(registrationController)
}
@Test
fun `when creating user with successful insertion, we return response user body`() = withBaseTestApplication {
val userId = 11
val responseUser = givenAResponseUser(userId)
coEvery { registrationController.createUser(any()) } returns responseUser
val body = toJsonBody(givenPostUserBody())
val call = handleRequest(HttpMethod.Post, "/user") {
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(body)
}
with(call) {
assertThat(HttpStatusCode.OK).isEqualTo(response.status())
val responseBody = response.parseBody(ResponseUser::class.java)
assertThat(responseUser).isEqualTo(responseBody)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment