Stub web calls in Scala
I'm currently writing a wrapper of the Spotify Metadata API to learn 开发者_开发知识库Scala. Everything's fine and dandy but I'd like to unit test the code. To properly do this I'll need to stub the Spotify API and get consistent return values (stuff like popularity of tracks changes very frequently).
Does anybody know how to stub web calls in Scala, the JVM in general or by using some external tool I could hook up into my Maven setup?
PS I'm basically looking for something like Ruby's FakeWeb...
Thanks in advance!
Sounds like you need a mocking framework to stub out the web-related calls (and layer your software appropriately to plug in either the real web framework or your mocked equivalent). JMock and Mockito both work well.
You'll need to use them in conjunction with a test framework such as Junit or TestNG. I've used all of these with Scala and they work as expected.
You can either mock your data service (the one that actually does the call) using Mockito or other mock framework (See an example here: http://guilhebl.github.io/scala/backend/testing/2017/07/12/scala-play-test-mock-guice-inject/ ) or you can stub the Router using a fake result coming from the resulting call to your Web service:
https://www.playframework.com/documentation/2.6.x/ScalaTestingWebServiceClients
精彩评论