Last active
July 31, 2020 11:08
-
-
Save mathias21/9255c650744ed2ef1be0da876c7df8f0 to your computer and use it in GitHub Desktop.
KtorEasy routing testing example 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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