Testkit
Colossus has a test kit that includes some classes for simplifying tests for servers. Testkit is built on ScalaTest.
Setup
Make sure you add colossus-testkit
to your dependcies, using the same version as colossus itself. For example:
libraryDependencies += "com.tumblr" %% "colossus-testkit" % "LATEST_VERSION" % "test
Unit Testing
Testing Callbacks
colossus.testkit.CallbackAwait
works similarly to Scala’s Await
for futures.
Testing Request Handlers
You can use MockConnection
to create a fake ServerContext
and create an instance of a request handler:
object TestkitExample extends App {
implicit val actorSystem = ActorSystem()
implicit val ioSystem = IOSystem()
HttpServer.start("example-server", 9000) {
new Initializer(_) {
override def onConnect = new MyHandler(_)
}
}
}
class MyHandler(context: ServerContext) extends RequestHandler(context) {
override def handle: PartialHandler[Http] = {
case request @ Get on Root / "foo" => request.ok("bar")
}
}
Todo, fix the spec!
/*class TestkitExampleSpec extends WordSpec with MustMatchers {
"request handler" must {
"generate a response" in {
val connection = MockConnection.server(new MyHandler(_))
val response = connection.typedHandler.handle(HttpRequest.get("/foo"))
CallbackAwait.result(response, 1.second).body.bytes.utf8String mustBe "hello"
}
}
}*/
Integration Testing
ColossusSpec
is a scalatest-based integration testing suite that contains a bunch of useful functions that help with spinning up instances of a service for testing
withIOSystem(f: IOSystem => Unit)
will spin up a new IOSystem
for the duration of a test and shut it down at the end.
withServer(server: ServerRef)(f: => Unit)
will shutdown the given server after the test completes