Giter Site home page Giter Site logo

rest-driver-scala's Introduction

rest-driver-scala

Build Status

Scala syntactic sugar and ScalaTest matchers for rest-driver.

Note that you can use the core Java rest-driver library directly from Scala just fine - this library just provides a little syntactic sugar to streamline your tests.

Usage

rest-driver-scala does not bundle rest-driver or scalatest so you will need to depend on both rest-driver-scala, rest-driver and scalatest. In your sbt build include:

resolvers += Resolver.bintrayRepo("kelveden", "maven")

libraryDependencies ++= Seq(
  "com.kelveden" %% "rest-driver-scala" % "1.0.1" % "test",
  "com.github.rest-driver" % "rest-client-driver" % "2.0.0" % "test",
  "com.github.rest-driver" % "rest-server-driver" % "2.0.0" % "test",
  "org.scalatest" %% "scalatest" % "3.0.1" % "test"
)

Now in your scalatest spec:

import org.scalatest.{FunSpec, Matchers}
import com.github.restdriver.serverdriver.RestServerDriver.

class RestDrivenTest extends FunSpec with Matchers with RestDriven with RestDrivenMatchers {
  override val restDriverPort = <PORT THAT RESTDRIVER WILL LISTEN ON>
  val servicePort = <PORT YOUR SERVICE UNDER TEST LISTENS ON>
  val baseUrl = s"http://localhost:$servicePort"
      
  describe("my service") {
    it("calls my stubbed service") {
      expect(onGetTo("/my/stubbed/service"), respondWith(200))

      val response = RestServerDriver.get(s"$baseUrl/my/service/under/test")

      response should haveStatus(200)
    }
  }
}

For more examples of usage see the unit tests.

Matchers

A simple set of scalatest matchers are available to match against a rest-driver Response object. Just mix in the RestDrivenMatchers trait:

haveBodyThat(bodyMatcher: Matcher[String])
haveBodyContent(content: String)
haveStatus(status: Int)
haveHeader(name: String)
haveHeader(name: String, value: String)

See the unit tests for examples of usage.

Logging

rest-driver provides some useful logging at INFO level via SLF4J. So, it's recommended that you provide some sort of SLF4J binding in your project for your tests.

You can see an example of using Logback to provide such a binding in this very library.

Using the Java API

As mentioned above, this library is simply syntactic sugar. So you can still leverage the core Java API as little or as much as you like.

In particular, the onXXXTo (onGetTo et al) and respondWith functions return a Java ClientDriverRequest and ClientDriverResponse object respectively that can be manipulated in the usual way. So, for example:

expect(
  onGetTo("/my/stubbed/service").withHeader("myheader1", "myvalue1"),
  respondWith(200).withHeader("myheader2", "value2")
)

Publishing

The module is published to bintray.

  1. Bump the version number and push to git.
  2. sbt +publish

(Run sbt bintrayChangeCredentials to setup bintray credentials if they are missing.)

rest-driver-scala's People

Contributors

kelveden avatar

Stargazers

Joe Littlejohn avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.