Hi, I'm working through the Ktor documentation and...
# ktor
p
Hi, I'm working through the Ktor documentation and example Task API and curiously tried to use the just announced Ktor 3.0.0-beta-2. I get the following error when at section: https://ktor.io/docs/3.0.0-beta-1/server-create-restful-apis.html#create-unit-tests Execution failed for task ':compileTestKotlin'.
Could not resolve all files for configuration ':testCompileClasspath'.
> Could not find io.ktorktor server test base3.0.0-beta-2. Searched in the following locations: - https://repo.maven.apache.org/maven2/io/ktor/ktor-server-test-base/3.0.0-beta-2/ktor-server-test-base-3.0.0-beta-2.pom Required by: project : > io.ktorktor server tests jvm3.0.0-beta-2 Possible solution: - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html Any suggestions? Thanks!
a
To solve the problem, replace the
io.ktor:ktor-server-tests-jvm
artifact with
io.ktor:ktor-server-test-host
in the Gradle build configuration:
Copy code
testImplementation("io.ktor:ktor-server-test-host")
p
Thank you Aleksei, this solves the issue! But I guess more steps are needed to get these tests working. Now I get the following: Expected response body of the type 'class kotlin.collections.List' but was 'class io.ktor.utils.io.SourceByteReadChannel' In response from
<http://localhost/tasks/byPriority/Medium>
Response status
404 Not Found
Response header
ContentType: null
Request header
Accept: */*
You can read how to resolve NoTransformationFoundException at FAQ: https://ktor.io/docs/faq.html#no-transformation-found-exception io.ktor.client.call.NoTransformationFoundException: Expected response body of the type 'class kotlin.collections.List' but was 'class io.ktor.utils.io.SourceByteReadChannel' In response from
<http://localhost/tasks/byPriority/Medium>
Response status
404 Not Found
Response header
ContentType: null
Request header
Accept: */*
a
Please check that the ContentNegotiation plugin is installed.
p
For Serialization.kt it is:
Copy code
fun Application.configureSerialization() {
    install(ContentNegotiation) {
        json()
    }
    routing {
        get("/json/kotlinx-serialization") {
            call.respond(mapOf("hello" to "world"))
        }
    }
}
But for ApplicationTest.kt it fails to recognize install using the same imports:
Copy code
class ApplicationTest {
    @Test
    fun tasksCanBeFoundByPriority() = testApplication {
        val client = createClient {
            testApplication.install(ContentNegotiation) {
                json()
            }
        }
    }
(I used the IntelliJ Ultimate Ktor plugin to generate the project with ContentNegotiation plugin. Maybe this project template is not updated for the latest Ktor version)
a
The project template should work for the new version of Ktor. Can you try to use the following code instead:
Copy code
@Test
fun tasksCanBeFoundByPriority() = testApplication {
    val client = createClient {
        install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) {
            json()
        }
    }
}
p
Thanks Aleksei, two things I learned (also with help from Bruce Hamilton): • there exists both dependencies on client and server content-negotiation (I missed the client version for testing) • when doing Ktor programmatic configuration no module is loaded in testApplication, so I had to add this line in each test:
Copy code
application { module() }
342 Views