diff options
-rw-r--r-- | core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala | 17 | ||||
-rw-r--r-- | docs/testing.rst | 12 |
2 files changed, 19 insertions, 10 deletions
diff --git a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala index 22ceff3..fbf7a0b 100644 --- a/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala +++ b/core/src/test/scala/com/softwaremill/sttp/testing/SttpBackendStubTests.scala @@ -159,18 +159,15 @@ class SttpBackendStubTests extends FlatSpec with Matchers with ScalaFutures { it should "not hold the calling thread when passed a future monad" in { val LongTimeMillis = 10000L - - val fm = new FutureMonad() - val f = Future { - Thread.sleep(LongTimeMillis) - Response(Right("OK"), 200, "", Nil, Nil) - } - val before = System.currentTimeMillis() - implicit val s = SttpBackendStub(fm).whenAnyRequest - .thenRespondWithMonad(f) - val result = sttp + implicit val s = SttpBackendStub(new FutureMonad()).whenAnyRequest + .thenRespondWithMonad(Future { + Thread.sleep(LongTimeMillis) + Response(Right("OK"), 200, "", Nil, Nil) + }) + + sttp .get(uri"http://example.org") .send() diff --git a/docs/testing.rst b/docs/testing.rst index bb2e3d5..ead722b 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -49,6 +49,18 @@ It is also possible to match requests by partial function, returning a response. This approach to testing has one caveat: the responses are not type-safe. That is, the stub backend cannot match on or verify that the type of the response body matches the response body type requested. +Another way to specify the behaviour is passing a response monad to Stub. It is useful if you need to test scenario with slow server, when response should be not returned immediately but after some time. +Example with Futures: :: + + implicit val testingBackend = SttpBackendStub(new FutureMonad()).whenAnyRequest + .thenRespondWithMonad(Future { + Thread.sleep(5000) + Response(Right("OK"), 200, "", Nil, Nil) + }) + + val respFuture = sttp.get(uri"http://example.org").send() + // responseFuture will complete after 10 seconds with "OK" response + Simulating exceptions --------------------- |