Dani Morillas
12/07/2022, 8:43 AM@Test
fun `when user user exists returns code 490`() : Unit = testApplication {
val client = createClient {
install(ContentNegotiation) {
json()
}
}
RepositoriesProvider.userRepository.add(User("", "username", "", false, 0, 0))
val response = <http://client.post|client.post>("/signup") {
contentType(ContentType.Application.Json)
setBody(UserRequest("username", "password"))
}
assertThat(response.status).isEqualTo(HttpStatusCode.Conflict)
}
to test the end point. I define the testApplication and the client to install the ContentNegotiation. But I have two questions here:
1. Do I need to repeat the createClient code in every method? Is there any way I can wrap the testApplication with this initialization and use it in all the tests.
2. The instance of the userRepository object is different in this test than in the code when the endpoint is reached. Am I missing something in the initialization so I can use the same instance?
Thanks in advance!Aleksei Tirman [JB]
12/07/2022, 11:34 AMtestApplication
function and configure a client.
2. Please try to switch the development mode off:
@Test
fun test() = testApplication {
environment {
developmentMode = false
}
// ...
}
Dani Morillas
12/07/2022, 1:41 PMfun testApplicationWrapper(block: suspend ApplicationTestBuilder.() -> Unit) {
testApplication {
environment {
developmentMode = false
}
createClient {
install(ContentNegotiation) {
json()
}
}
}
}
and then something like
@Test
fun test() : Unit = testApplicationWrapper {
...
}
?Aleksei Tirman [JB]
12/07/2022, 2:10 PMfun myTest(test: suspend (HttpClient) -> Unit) = testApplication {
environment {
developmentMode = false
}
val client = createClient {
install(ContentNegotiation) {
json()
}
}
test(client)
}
Aleksei Tirman [JB]
12/07/2022, 2:10 PM@Test
fun test() = myTest { client ->
assertEquals("OK", client.get("/").bodyAsText())
}
Dani Morillas
12/07/2022, 2:12 PM