Created
January 29, 2019 09:11
-
-
Save joakim-ribier/5d528d69805e3d9bd4628984559840e2 to your computer and use it in GitHub Desktop.
scalamock - Mocking_style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.Future | |
| import org.scalatest.{MustMatchers, WordSpec} | |
| import org.scalamock.scalatest.MockFactory | |
| trait AdditionHttpService { | |
| def add(arg0: Int, arg1: Int): Future[Int] | |
| } | |
| class AdditionService(additionHttpService: AdditionHttpService) { | |
| def add(arg0: Int, arg1: Int): Int = arg0 + arg1 | |
| def addF(arg0: Int, arg1: Int): Future[Int] = additionHttpService.add(arg0, arg1) | |
| } | |
| class AdditionServiceSpec extends WordSpec with MustMatchers with MockFactory { | |
| "Addition service" when { | |
| "add" must { | |
| "adds two positive values" in { | |
| val service = new AdditionService(null) | |
| val result = service.add(1, 2) | |
| result mustBe 3 | |
| } | |
| "adds positive and negative values" in { | |
| val service = new AdditionService(null) | |
| val result = service.add(1, -2) | |
| result mustBe -1 | |
| } | |
| } | |
| "addF" must { | |
| "adds two positive values [stub]" in { | |
| // mock service | |
| val additionHttpServiceMock = stub[AdditionHttpService] | |
| // record | |
| (additionHttpServiceMock.add _).when(*, *).returns(Future.successful(3)) | |
| val service = new AdditionService(additionHttpServiceMock) | |
| // then | |
| service.addF(1, 2) | |
| // verify | |
| (additionHttpServiceMock.add _).verify(1, 2) | |
| } | |
| "adds positive and negative values [mock]" in { | |
| // mock service | |
| val additionHttpServiceMock = mock[AdditionHttpService] | |
| // set expectation before | |
| (additionHttpServiceMock.add _).expects(1, -2).returns(Future.successful(3)) | |
| val service = new AdditionService(additionHttpServiceMock) | |
| service.addF(1, -2) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment