mudasar187
06/10/2024, 2:18 PMTestApplication
?
My restassured test:
val response =
RestAssured.given()
.filter(validationFilter)
.header(HttpHeaders.ContentType, APPLICATION_JSON)
.header(HttpHeaders.Authorization, "Bearer ${mockOAuth2Server.token()}")
.body(OppdragsInfoRequest(gjelderId = "12345678901", fagGruppeKode = "ABC"))
.port(PORT)
.post("$OPPDRAGSINFO_BASE_API_PATH/oppdragsinfo")
.then()
.assertThat()
.statusCode(HttpStatusCode.OK.value)
.extract()
.response()
Any my TestApplicationListener
(using kotest):
object EmbeddedTestServerListener : TestListener {
val oppdragsInfoService = mockk<OppdragsInfoService>()
val integrationService = mockk<IntegrationService>()
var authorization by Delegates.notNull<Boolean>()
val mockOAuth2Server = MockOAuth2Server()
lateinit var testApplication: TestApplication
operator fun invoke(authorization: Boolean): TestListener {
this.authorization = authorization
return this
}
override suspend fun beforeSpec(spec: Spec) {
mockOAuth2Server.start()
this.testApplication =
TestApplication {
application {
myApplicationModule()
}
}
testApplication.start()
}
override suspend fun afterSpec(spec: Spec) {
mockOAuth2Server.shutdown()
}
}
private fun Application.myApplicationModule() {
commonConfig()
if (authorization) {
securityConfig(true, mockOAuth2Server.mockAuthConfig())
}
routing {
authenticate(authorization, AUTHENTICATION_NAME) {
integrationApi(integrationService)
oppdragsInfoApi(oppdragsInfoService)
}
}
}
When running test I get following output:
15:56:40 [] [ker-1] INFO ktor.test
: Responding at <http://localhost:80>
15:56:40 [] [ker-1] INFO ktor.test
: Responding at <https://localhost:443>
Connection refused
java.net.ConnectException: Connection refused
The PORT
is a constant variable set to 80
just to check if I can response but I get connection refused. Or I cant use TestApplication in my case? do i need to spin up a EmbeddedServer when doing Restassured tests ?Aleksei Tirman [JB]
06/12/2024, 7:30 AMtestApplication
doesn't use a network for testing, so the test server doesn't listen for connections, and the test client doesn't establish TCP/IP connections. As far as I understand, REST Assured requires an HTTP server listening for connections to test it so you can't use the testApplication
with REST Assured.