web-servicesscalascalatra

Most lightweight webservice framework to mock external Webservice in Scala


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?


Solution

  • 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.