【问题标题】:Ktor: testing REST endpoints using Spek/KotlinTest instad of JUnit Test ClassKtor:使用 JUnit 测试类的 Spek/KotlinTest 测试 REST 端点
【发布时间】:2018-07-23 12:17:37
【问题描述】:

我有一个简单的 hello world Ktor 应用程序:

fun Application.testMe() {
  intercept(ApplicationCallPipeline.Call) {
    if (call.request.uri == "/")
      call.respondText("Hello")
    }
}

使用 JUnit 测试类,我可以为它编写测试,如其文档中所述;如下:

class ApplicationTest {
    @Test fun testRequest() = withTestApplication(Application::testMe) {
        with(handleRequest(HttpMethod.Get, "/")) {
            assertEquals(HttpStatusCode.OK, response.status())
            assertEquals("Hello", response.content)
        }
        with(handleRequest(HttpMethod.Get, "/index.html")) {
            assertFalse(requestHandled)
        }
    }
}

但是,我想在 Spek 或 KotlinTest 中进行单元测试,无需 JUnit 的帮助,类似于我在 ScalaTest/Play 中进行的方式;以更具声明性的方式:

  1. 在测试期间向路由(即/)发送 FakeRequest。
  2. 获取页面内容,并检查字符串“hello”。

问题是我可以在 KotlinTest 或 Spek 中以更具声明性的方式编写上述测试吗?

【问题讨论】:

    标签: ktor spek kotlintest


    【解决方案1】:

    首先关注spek setup guide with JUnit 5

    然后你可以像下面这样简单地声明你的规范

    object HelloApplicationSpec: Spek({
        given("an application") {
            val engine = TestApplicationEngine(createTestEnvironment())
            engine.start(wait = false) // for now we can't eliminate it
            engine.application.main() // our main module function
    
            with(engine) {
                on("index") {
                    it("should return Hello World") {
                        handleRequest(HttpMethod.Get, "/").let { call ->
                            assertEquals("Hello, World!", call.response.content)
                        }
                    }
                    it("should return 404 on POST") {
                        handleRequest(HttpMethod.Post, "/", {
                            body = "HTTP post body"
                        }).let { call ->
                            assertFalse(call.requestHandled)
                        }
                    }
                }
            }
        }
    })
    

    这是我的 build.gradle(简化版)

    buildscript {
        dependencies {
            classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
        }
    
        repositories {
            jcenter()
        }
    }
    
    repositories {
        jcenter()
        maven { url "http://dl.bintray.com/jetbrains/spek" }
    }
    
    apply plugin: 'org.junit.platform.gradle.plugin'
    
    junitPlatform {
        filters {
            engines {
                include 'spek'
            }
        }
    }
    
    dependencies {
        testCompile 'org.jetbrains.spek:spek-api:1.1.5'
        testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.5'
        testCompile "io.ktor:ktor-server-test-host:$ktor_version"
    }
    

    【讨论】:

    • 感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多