forked from leanovate/play-mockws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.scala
94 lines (69 loc) · 2.38 KB
/
Example.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package mockws
import org.scalatest.{OptionValues, FreeSpec, Matchers}
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.ws.WSClient
import play.api.mvc.Action
import play.api.mvc.Results._
import play.api.test.Helpers._
import scala.concurrent.Future
import scala.util.Try
class Example extends FreeSpec with Matchers with OptionValues {
object ImplementationToTest {
// GatewayToTest simulates an gateway implementation we want to test
object GatewayToTest {
val userServiceUrl = "http://userservice"
}
/** @param ws [[WSClient]] as dependency injection */
class GatewayToTest(ws: WSClient) {
import GatewayToTest.userServiceUrl
/** @return age of the user if known */
def age(userId: Long): Future[Option[Int]] =
ws.url(s"$userServiceUrl/users/$userId/age").get().map {
case response if response.status == 200 => Try(response.body.toInt).toOption
case _ => None
}
}
}
trait TestScope {
import ImplementationToTest._
val userServiceUrl = GatewayToTest.userServiceUrl
def userRoute: Route
def ageResponse(userId: Long) = {
// we initialize a mock WS with the defined route
val ws = MockWS(userRoute)
// we inject the MockWS into GatewayToTest
val testedGateway = new GatewayToTest(ws)
try await(testedGateway.age(userId))
finally ws.close()
}
}
// and we can test the implementation of GatewayToTest
"GatewayToTest.age should" - {
"return None" - {
"when the user service does not know the user" in new TestScope {
override val userRoute = Route {
case (GET, u) if u == s"$userServiceUrl/users/23/age" => Action {
NotFound("user 23 not known")
}
}
ageResponse(userId = 23) shouldEqual None
}
"when the user service response is not an Integer" in new TestScope {
override val userRoute = Route {
case (GET, u) if u == s"$userServiceUrl/users/27/age" => Action {
Ok("crap")
}
}
ageResponse(userId = 27) shouldEqual None
}
}
"return the age of the user" in new TestScope {
override val userRoute = Route {
case (GET, u) if u == s"$userServiceUrl/users/5/age" => Action {
Ok("67")
}
}
ageResponse(userId = 5).value shouldEqual 67
}
}
}