├── project └── build.properties ├── README.md └── src └── test └── scala └── uk └── co └── deku └── wiremock ├── ScalaTest.scala └── Specs2.scala /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.0 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WireMock Scala Examples 2 | ======================= 3 | 4 | Examples of integrating [WireMock](http://wiremock.org) with Scala testing frameworks. 5 | 6 | Test frameworks 7 | --------------- 8 | 9 | * [Specs2](http://etorreborre.github.io/specs2/) - [Specs2.scala](https://github.com/danmassie/wiremock-scala-examples/blob/master/src/test/scala/uk/co/deku/wiremock/Specs2.scala) 10 | * [ScalaTest](http://www.scalatest.org/) - [ScalaTest.scala](https://github.com/danmassie/wiremock-scala-examples/blob/master/src/test/scala/uk/co/deku/wiremock/ScalaTest.scala) 11 | -------------------------------------------------------------------------------- /src/test/scala/uk/co/deku/wiremock/ScalaTest.scala: -------------------------------------------------------------------------------- 1 | package uk.co.deku.wiremock 2 | 3 | import java.util.concurrent.TimeUnit 4 | 5 | import scala.concurrent.Await 6 | import scala.concurrent.ExecutionContext.Implicits.global 7 | import scala.concurrent.duration.Duration 8 | 9 | import org.scalatest.{ BeforeAndAfterEach, FlatSpec, Matchers } 10 | 11 | import com.github.tomakehurst.wiremock.WireMockServer 12 | import com.github.tomakehurst.wiremock.client.WireMock 13 | import com.github.tomakehurst.wiremock.client.WireMock._ 14 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration._ 15 | 16 | import dispatch.{ Http, url } 17 | 18 | class ScalaTest extends FlatSpec with Matchers with BeforeAndAfterEach { 19 | 20 | val Port = 8080 21 | val Host = "localhost" 22 | val wireMockServer = new WireMockServer(wireMockConfig().port(Port)) 23 | 24 | override def beforeEach { 25 | wireMockServer.start() 26 | WireMock.configureFor(Host, Port) 27 | } 28 | 29 | override def afterEach { 30 | wireMockServer.stop() 31 | } 32 | 33 | "WireMock" should "stub get request" in { 34 | val path = "/my/resource" 35 | stubFor(get(urlEqualTo(path)) 36 | .willReturn( 37 | aResponse() 38 | .withStatus(200))) 39 | val request = url(s"http://$Host:$Port$path").GET 40 | val responseFuture = Http(request) 41 | 42 | val response = Await.result(responseFuture, Duration(100, TimeUnit.MILLISECONDS)) 43 | response.getStatusCode should be(200) 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /src/test/scala/uk/co/deku/wiremock/Specs2.scala: -------------------------------------------------------------------------------- 1 | package uk.co.deku.wiremock 2 | 3 | import java.util.concurrent.TimeUnit 4 | import scala.concurrent.Await 5 | import scala.concurrent.ExecutionContext.Implicits.global 6 | import scala.concurrent.duration.Duration 7 | import org.specs2.mutable.{ BeforeAfter, Specification } 8 | import com.github.tomakehurst.wiremock.WireMockServer 9 | import com.github.tomakehurst.wiremock.client.WireMock 10 | import com.github.tomakehurst.wiremock.client.WireMock._ 11 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration._ 12 | import dispatch.{ Http, url } 13 | 14 | class Specs2 extends Specification { 15 | 16 | val Port = 8080 17 | val Host = "localhost" 18 | 19 | trait StubServer extends BeforeAfter { 20 | val wireMockServer = new WireMockServer(wireMockConfig().port(Port)) 21 | 22 | def before = { 23 | wireMockServer.start() 24 | WireMock.configureFor(Host, Port) 25 | } 26 | 27 | def after = wireMockServer.stop() 28 | } 29 | 30 | "WireMock" should { 31 | "stub get request" in new StubServer { 32 | val path = "/my/resource" 33 | stubFor(get(urlEqualTo(path)) 34 | .willReturn( 35 | aResponse() 36 | .withStatus(200))) 37 | 38 | val request = url(s"http://$Host:$Port$path").GET 39 | val responseFuture = Http(request) 40 | 41 | val response = Await.result(responseFuture, Duration(100, TimeUnit.MILLISECONDS)) 42 | response.getStatusCode mustEqual 200 43 | } 44 | } 45 | } --------------------------------------------------------------------------------