Testing
Our news feed application is ready, but it's not production ready until it is tested.
Adding Gradle dependency
We need to add JUnit and Ktor dependencies in the build.gradle
file to test the application:
// Testing testCompile group: 'junit', name: 'junit', version: '4.12' testCompile "io.ktor:ktor-server-test-host:$ktor_version"
Testing the application
We have three URL endpoints in our application, two of which return JSON data for which we will use an external tool named Postman to test them.
Testing the index URL
The preceding test case gets an instance of our application and sends a get request on the specified URL, which in this is /
, aka an index URL, and it checks if the status is OK - 200
and content is Hello readers!
:
class ApplicationTest { @Test fun `check index page`() = withTestApplication(Application::main) { with(handleRequest(HttpMethod.Get, "/")) { assertEquals(HttpStatusCode.OK, response.status()) assertEquals...