I have a Scalatra web service that runs with embedded Jetty. I'd now like to write integration tests that:
This should all be triggered by an SBT command.
How should I go about this?
You could write such integration tests on top of BDD test frameworks like Specs. Unfiltered project has many such examples that should work for other web frameworks like Scalatra.
For example, take a look at ServerSpec:
"A Server" should {
"respond to requests" in {
http(host as_str) must_== "test"
}
....
}
It's starting up a test server specified in setup
and hitting it using Dispatch in the specification. The key part is implemented in unfiltered.spec.jetty.Served
trait, which does that you described: starting and stopping the service. There's also Specs2 version: unfiltered.specs2.jetty.Served
.
Another trick you could use is sbt-revolver, which my favorite plugin while doing any web development, especially used in conjunction with JRebel. This plugin can load your web server in the background. I haven't tried test together, but it could work if you don't have to change the server-side during the test.
Scalatra offers a DSL to write tests. There is support for specs2 and scalatest.
By default an embedded Jetty will be used for testing. If you want to provide your own server, you can reuse the EmbeddedJettyContainer
implementation and override start
, stop
and servletContextHandler
.
start
will be called before executing the tests, which allows to start your server if required. stop
is called after the tests. servletContextHandler
is required in order to add your servlets using addServlet(..)
.
This is from the spec2 integration:
trait BaseScalatraSpec extends SpecificationStructure with FragmentsBuilder with ScalatraTests {
override def map(fs: =>Fragments) = Step(start()) ^ super.map(fs) ^ Step(stop())
}
trait ScalatraTests extends EmbeddedJettyContainer with HttpComponentsClient { }
Alternatively you can provide your own Container implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With