3

I am trying to test a component which relies on an external webservice, which I access through Play WS library. This components receive the url of the webservice.

I would like to unit test the component by getting it connected to a fake webservice.

Which scala web frameworks would be more suitable for the purpose?

Edmondo
  • 19,559
  • 13
  • 62
  • 115

1 Answers1

4

I failed to find something simplier than scalatra. Although code on main page is really simple you would have to do some extra work to embed scalatra in your own app/tests.

import org.scalatra._
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.webapp.WebAppContext

private class Mocker extends ScalatraServlet {
    get("/somepath") {
      <h1>Mocked response</h1>
    }
  }

// ↓ you won't need that part if you start jetty as sbt command

private val jetty = new Server(8080)
private val context = new WebAppContext()
context setContextPath "/"
context setResourceBase "/tmp"
context addServlet(classOf[Mocker], "/*")

jetty.setHandler(context)
jetty.start

Standalone app is really that simple.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • is there anything similar with play? – Edmondo Apr 23 '13 at 08:42
  • @Edmondo1984 they both work in Web :-) Since you're going to use WS library [to my knowledge] it doesn't matter how well play and scalatra interact with each other (you might even use sinatra or django on the test side and you wouldn't get the difference on the other end). – om-nom-nom Apr 23 '13 at 08:53
  • I totally agree... I am just trying to avoid multiplying the frameworks on which my app relies... – Edmondo Apr 23 '13 at 08:57
  • @Edmondo1984 looks like guardian [build special library](https://github.com/guardian/integration-tools) for such purpose – om-nom-nom May 20 '13 at 14:15
  • @om-nom-nom can you please include the imports? – Joey Baruch May 15 '18 at 12:11