Back to directory
leanovate avatar

play-mockws

Mock WS client for Play Framework. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/leanovate/play-mockws.git

141

Stars

33

Forks

20

Watchers

MIT

License

Play MockWS

CI Workflow Maven Central

Goal

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.

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.

Compatibility Matrix

play-mock-ws version Play versions Scala versions
3.1.x 2.9, 3.0 2.13, 3.3
3.0.x 2.8, 2.9, 3.0 2.12, 2.13, 3.3
2.9.x 2.9 2.13, 3.3
2.8.x 2.8 2.12, 2.13
2.7.1 2.7 2.11, 2.12, 2.13
2.7.0 2.7 2.11, 2.12
2.6.x 2.6 2.11, 2.12
2.5.x 2.5 2.11
2.4.x 2.4 2.10, 2.11
2.3.x 2.3 2.10, 2.11

play-mockws 3+

// Play 3.0.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws-3-0" % "x.y.z" % Test

// Play 2.9.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws-2-9" % "x.y.z" % Test

play-mockws 2.x

Note that before the version 3.x, play-mockws was following a different naming and versioning scheme:

// Play 2.9.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.9.0" % Test

// Play 2.8.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.8.0" % Test

// Play 2.7.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.7.1" % Test

// Play 2.6.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.6.6" % Test

// Play 2.5.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.5.2" % Test

// Play 2.4.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.4.2" % Test

// Play 2.3.x
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.3.2" % Test

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.

Release notes

See RELEASE-NOTES.md or GitHub releases.

Releases

May 22, 2026

Download .zip

What's Changed

Update play, play-ahc-ws, play-test to 3.0.11 Update play, play-ahc-ws, play-test to 2.9.11

Full Changelog: v3.1.2...v3.1.3

Dec 29, 2025

Download .zip

What's Changed ℹ️ 🪪 A new GPG key is used to sign the artifacts we publish to Central. If you happen to check it, the new key is 006145654D78FDE8.

Update play, play-ahc-ws, play-test to 2.9.10 by @s...

Dec 10, 2025

Download .zip

⚠️ v3.1.1 was not published to Maven Central repository. We'll publish a v3.1.2 soon. What's Changed

Upgrade Scala to 2.13.18 and 3.3.7

Full Changelog: v3.1.0...v3.1.1

Sep 14, 2025

Download .zip

What's Changed

Update Play to 2.9.9/3.0.9 Stop publishing play-mockws-2-8 (for Play 2.8.x)

Others

chore(deps): update actions/checkout action to v5 by @renovate[bot] in #581 Update sbt-ci-release t...

What's Changed

Update Play to 2.9.8/3.0.8 Update SBT to 1.11.3

Full Changelog: v3.0.8...v3.0.9