Skip to content

Instantly share code, notes, and snippets.

@manjotmona

manjotmona/RestSpec.scala

Last active Jun 10, 2018
Embed
What would you like to do?
class RestSpec extends WordSpec with Matchers with ScalatestRouteTest with MockitoSugar {
val mockUserImpl = mock[UserImpl]
object TestObject extends RestService {
val userImpl = mockUserImpl
}
"The service" should {
"return user added as response for a Post request to /adduser" in {
when(mockUserImpl.addUser(User(2, "test"))).thenReturn(Future.successful(Done))
val jsonRequest = ByteString(
s"""
|{
| "id":2,
| "name":"test"
|}
""".stripMargin)
val postRequest = HttpRequest(
HttpMethods.POST,
uri = "/adduser",
entity = HttpEntity(MediaTypes.`application/json`, jsonRequest))
postRequest ~> Route.seal(TestObject.route) ~> check {
status.isSuccess() shouldEqual true
responseAs[String] shouldEqual "user added"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment