Justin Xu
10/17/2022, 10:15 PM@Test
fun testBasicCreate() = testApplication {
application {
configureSerialization()
}
routing {
userRouting()
}
val client = createClient {
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
isLenient = true
prettyPrint = true
},
contentType = ContentType.Application.Json
)
}
}
...
test code
...
}
where everything before the actual test code is duplicated at the start of each test. Is there a way to put this code in one place and remove the duplicates? Thanksephemient
10/17/2022, 10:38 PMfun TestApplicationBuilder.commonSetup() {
...
}
@Test
fun testBasicCreate() = testApplication {
commonSetup()
...
}
or
fun myTestApplication(block: TestApplicationBuilder.() -> Unit) = testApplication {
...
block()
}
.
@Test
fun testBasicCreate() = myTestApplication {
...
}
Justin Xu
10/18/2022, 1:35 AMAleksei Tirman [JB]
10/18/2022, 10:00 AMtestApplication
function:
fun testApp(block: suspend (HttpClient) -> Unit) = testApplication {
application {
configureSerialization()
}
routing {
userRouting()
}
val client = createClient {
install(ContentNegotiation) {
json(
Json {
ignoreUnknownKeys = true
isLenient = true
prettyPrint = true
},
contentType = ContentType.Application.Json
)
}
}
block(client)
}
Here is a simple test:
@Test
fun testBasicCreate() = testApp { client ->
assertEquals("OK", client.get("/").bodyAsText())
}
Justin Xu
10/19/2022, 1:53 AM