/javalin-test

An Acceptance Testing tool for the Javalin Web Framework

Primary LanguageKotlinApache License 2.0Apache-2.0

Build Status License

Javalin Test - An Acceptance Testing tool for the Javalin Web Framework

javalin-test is a tool that will launch a short-lived javalin server in your tests. From here, you can register your request handlers and use the built-in http client to test them.

Install

javalin-test is available via Jitpack. Click the badge for instructions on adding it to your project.

Javalin 3 or greater is required.

Usage

With Kotlin:

class KotlinTest {
    @Test
    fun get() {
        // Start a javalin server
        JavalinTest.test { app, client ->
            // Register a request handler
            app.get("/") { it.result("javalin") }

            // Call with the built-in http client
            client.get("/").use { resp ->
            
                // Test the response
                Assert.assertThat(resp.code, CoreMatchers.equalTo(200))
                Assert.assertThat(resp.body?.string(), CoreMatchers.equalTo("javalin"))
            }
        }
    }
}

With Java:

public class JavaTest {
   @Test
   public void get() {
       // Start a javalin server
       JavalinTest.test((app, client) -> {
           // Register a request handler
           app.get("/", ctx -> ctx.result("javalin"));
           
           // Call with the built-in http client
           final Response resp = client.get("/");
           
           // Test the response
           Assert.assertThat(resp.code(), CoreMatchers.equalTo(200));
           Assert.assertThat(resp.body().string(), CoreMatchers.equalTo("javalin"));
       });
   }
}

See more examples in Kotlin and Java.