gpt4 book ai didi

ktor - 如何以编程方式启动 ktor-server 以进行集成测试

转载 作者:行者123 更新时间:2023-12-05 02:55:25 33 4
gpt4 key购买 nike

KTor 与单元测试配合得很好,但我如何启动我的 ktor 服务器进行集成测试?

我的测试是这样的:

@ExperimentalCoroutinesApi
class HttpClientTest {
private val logger = LoggerFactory.getLogger(HttpClientTest::class.java.name)

companion object {

@BeforeClass
@JvmStatic
fun setup() {
// ?????
}

@AfterClass
@JvmStatic
fun teardown() {
// ????
}
}


@Test
fun testMitDefaultHttpClient() {
val httpget = HttpGet("http://0.0.0.0:8080/json/gson")
httpget.setHeader("Accept", "application/json")
httpget.setHeader("Content-Type", "application/json")

val httpclient: HttpClient = HttpClients.createDefault()
val response = httpclient.execute(httpget)

Assert.assertEquals(200, response.statusLine.statusCode.toLong())

val responseString = EntityUtils.toString(response.entity)
logger.info(responseString)
}
}

最佳答案

好的 - 找到解决方案:

package at.mikemitterer.catshostel.routes

import at.mikemitterer.catshostel.WsClientApp.main
import at.mikemitterer.catshostel.main
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.applicationEngineEnvironment
import io.ktor.server.engine.connector
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.netty.NettyApplicationEngine
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import org.junit.*
import org.koin.test.KoinTest
import org.slf4j.LoggerFactory


@ExperimentalCoroutinesApi
class HttpClientTest : KoinTest {
private val logger = LoggerFactory.getLogger(HttpClientTest::class.java.name)


companion object {
lateinit var server: NettyApplicationEngine

@BeforeClass
@JvmStatic
fun setup() {
val env = applicationEngineEnvironment {
module {
main(true)
}
// Public API
connector {
host = "0.0.0.0"
port = 8080
}
}
server = embeddedServer(Netty, env).start(false)
}

@AfterClass
@JvmStatic
fun teardown() {
// clean up after this class, leave nothing dirty behind
server.stop(1000, 10000)
}
}


@Test
fun testMitDefaultHttpClient() {
val httpget = HttpGet("http://0.0.0.0:8080/json/gson")
httpget.setHeader("Accept", "application/json")
httpget.setHeader("Content-Type", "application/json")

val httpclient: HttpClient = HttpClients.createDefault()
val response = httpclient.execute(httpget)

Assert.assertEquals(200, response.statusLine.statusCode.toLong())

val responseString = EntityUtils.toString(response.entity)
logger.info(responseString)
}
}

关于ktor - 如何以编程方式启动 ktor-server 以进行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61245981/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com