Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Scala application in SBT for integration test

I have a Scalatra web service that runs with embedded Jetty. I'd now like to write integration tests that:

  • start the service (using the main method of the application)
  • run the tests (driving the HTTP interface)
  • stop the service.

This should all be triggered by an SBT command.

How should I go about this?

like image 686
thomson_matt Avatar asked Oct 21 '22 02:10

thomson_matt


2 Answers

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.

like image 80
Eugene Yokota Avatar answered Oct 29 '22 15:10

Eugene Yokota


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.

like image 38
Stefan Ollinger Avatar answered Oct 29 '22 14:10

Stefan Ollinger