Abdulrahman Al Sabagh (Abudi)
09/19/2024, 6:46 PMtestApp = TestApplication {
application {
configureApp(Config.forTest())
}
}
private val appClient = testApp.createClient {
install(ContentNegotiation) {
json()
}
}
@BeforeTest
fun setup() {
runBlocking {
appClient.get("/")
repository.deleteAll()
}
}
@Test
fun `getAll users`(): Unit = runBlocking {
appClient.get("/user").apply {
assertEquals(HttpStatusCode.OK, status)
assertEquals(emptyList<List<User>>(), body<List<User>>())
}
repository.create(User("bob", "bob@example.com", "bobby"))
val result2 = appClient.get("/user").body<List<User>>()
assertEquals(repository.getAll().toString(), result2.toString())
}
@Test
fun `given a user, when sending a post request, then check if the user is saved in the repository`(): Unit =
testApplication {
val newUser = User(name = "franz", email = "franz@example.com", password = "12354678")
sendPostRequestToUser(newUser)
.apply {
assertEquals(HttpStatusCode.Created, status)
assertTrue(repository.contains(newUser))
}
}
@Test
fun `given the same user twice, when sending 2 post requests, return Created then Conflict`() =
testApplication {
val newUser = User("bob", "bob@bob.bob", "bob")
sendPostRequestToUser(newUser).apply { assertEquals(HttpStatusCode.Created, status) }
sendPostRequestToUser(newUser).apply { assertEquals(HttpStatusCode.Conflict, status) }
}
ok at the beforeTest annotation
I am somehow forced to always send a reqeust to the server
Without this request -> Koin can not be startedAleksei Tirman [JB]
09/20/2024, 8:28 AMstartApplication
method within the testApplication
to manually start the test server.Abdulrahman Al Sabagh (Abudi)
09/25/2024, 3:04 PM