All Projects → leanovate → Play Mockws

leanovate / Play Mockws

Licence: mit
Mock WS client for Play Framework

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Play Mockws

Bdp Dataplatform
大数据生态解决方案数据平台:基于大数据、数据平台、微服务、机器学习、商城、自动化运维、DevOps、容器部署平台、数据平台采集、数据平台存储、数据平台计算、数据平台开发、数据平台应用搭建的大数据解决方案。
Stars: ✭ 456 (+240.3%)
Mutual labels:  playframework
Cfp Devoxx
Call for Papers application created for Devoxx France, used by many conferences
Stars: ✭ 79 (-41.04%)
Mutual labels:  playframework
Play Circe
circe for play
Stars: ✭ 95 (-29.1%)
Mutual labels:  playframework
Play Slick
Slick Plugin for Play
Stars: ✭ 792 (+491.04%)
Mutual labels:  playframework
Play Json Schema Validator
JSON Schema Validation with Play JSON
Stars: ✭ 68 (-49.25%)
Mutual labels:  playframework
Play Slick3 Example
A simple skeleton for play scala slick applications.
Stars: ✭ 83 (-38.06%)
Mutual labels:  playframework
Atmosphere
Realtime Client Server Framework for the JVM, supporting WebSockets with Cross-Browser Fallbacks
Stars: ✭ 3,552 (+2550.75%)
Mutual labels:  playframework
Play Monadic Actions
A simple scala DSL to allow clean and monadic style for Play! Actions
Stars: ✭ 129 (-3.73%)
Mutual labels:  playframework
Hepek
Web content generators in Scala. Intuitive, scalable, powerful.
Stars: ✭ 69 (-48.51%)
Mutual labels:  playframework
Scalatestplus Play
ScalaTest + Play
Stars: ✭ 88 (-34.33%)
Mutual labels:  playframework
Play Zhewbacca
Play! framework library to protect REST endpoint by OAuth2 token verification. Supports Play versions 2.5, 2.6, 2.7
Stars: ✭ 21 (-84.33%)
Mutual labels:  playframework
Play Scala Slick Example
Example Play Scala project with Slick
Stars: ✭ 59 (-55.97%)
Mutual labels:  playframework
Play Java Websocket Example
Example Play Java application showing Websocket usage with Akka actors
Stars: ✭ 86 (-35.82%)
Mutual labels:  playframework
Twirl
Twirl is Play's default template engine
Stars: ✭ 498 (+271.64%)
Mutual labels:  playframework
Play Pdf
A PDF module for the Play framework
Stars: ✭ 108 (-19.4%)
Mutual labels:  playframework
Play Reactivemongo
🍃 ReactiveMongo plugin for Playframework
Stars: ✭ 411 (+206.72%)
Mutual labels:  playframework
Play Ebean
Play Ebean module
Stars: ✭ 82 (-38.81%)
Mutual labels:  playframework
Izanami
Izanami is a shared configuration, feature flipping and A/B testing server well-suited for micro-service architecture implementation.
Stars: ✭ 133 (-0.75%)
Mutual labels:  playframework
Play Jsmessages
Library to compute localized messages of your Play application on client side
Stars: ✭ 125 (-6.72%)
Mutual labels:  playframework
Sbt Newrelic
New Relic monitoring integration for SBT with sbt-native-packager
Stars: ✭ 87 (-35.07%)
Mutual labels:  playframework

Table of Contents

play-mockws

Build Status Codacy Badge Coverage Status Maven Central

Play MockWS is a mock WS client for Play Framework.

If:

  • you write an application in Scala with the Play Framework
  • the application makes HTTP calls to external web services with the WS client
  • you want to test your implementation

then you can use play-mockws to simulate HTTP requests to external web services in your tests.

Release Notes

see RELEASE-NOTES.md

Example

// simulation of a GET request to http://dns/url
val ws = MockWS {
  case (GET, "http://dns/url") => Action { Ok("http response") }
}

await(ws.url("http://dns/url").get()).body == "http response"

Adding play-mockws to your project

Add MockWS as test dependency in the build.sbt:

  • for Play 2.8.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.8.0" % Test
  • for Play 2.7.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.7.1" % Test
  • for Play 2.6.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.6.6" % Test
  • for Play 2.5.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.5.2" % Test
  • for Play 2.4.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.4.2" % Test
  • for Play 2.3.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.3.2" % Test

And use it:

import mockws.MockWS

val ws = MockWS { ... }

Usage

General usage

From the 2.6 version, it is recommended that your tests either extend trait MockWSHelpers or import MockWSHelpers. MockWSHelpers provides an implicit Materializer you need when working with Play's Actions.

class MySpec extends FreeSpec with Matchers with MockWSHelpers with BeforeAndAfterAll {
  ...

  override def afterAll(): Unit = {
    shutdownHelpers()
  }
}

or

import mockws.MockWSHelpers._

A MockWS instance can be directly constructed with a partial function like this:

val ws = MockWS {
  case (GET, "/") => Action { Ok("homepage") }
  case (POST, "/users") => Action { request => Created((request.body.asJson.get \ "id").as[String]) }
  case (GET, "/users/24") => Action { NotFound("") }
}

The partial function binds 2 Strings, an HTTP method and the URL, to a Play Action.

For clarity, this partial function is aliased as MockWS.Routes

When calling MockWS.url(), if the HTTP method and the URL are found, the defined play action is evaluated.

Controlling the routes

If you want more control on the routes, for example to know whether a route was called or how many times, use the Route class for this.

Routes can be defined together with the standard function orElse.

val route1 = Route {
  case (GET, "/route1") => Action { Ok("") }
}
val route2 = Route {
  case (GET, "/route2") => Action { Ok("") }
}

val ws = MockWS(route1 orElse route2)

await(ws.url("/route1").get())

route1.called == true
route2.called == false

route1.timeCalled == 1
route2.timeCalled == 0

An example how to structure an implementation to test it with MockWS can be found here.

Other examples can be found in the tests.

Compatibility Matrix

  • MockWS 2.6.x is actually only compatible with Play 2.6.y., with Scala 2.12 or 2.11.
  • MockWS 2.5.x is actually only compatible with Play 2.5.y., with Scala 2.11.
  • MockWS 2.4.x is actually only compatible with Play 2.4.y., with Scala 2.10 or 2.11.
  • MockWS 2.3.x is actually only compatible with Play 2.3.y., with Scala 2.10 or 2.11.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].