Scala 中的存根网络调用
我目前正在编写 Spotify 元数据 API 的包装器来学习 Scala。一切都很好,但我想对代码进行单元测试。为了正确地做到这一点,我需要存根 Spotify API 并获得一致的返回值(比如曲目的流行度变化非常频繁)。
有谁知道如何在 Scala、JVM 中存根 Web 调用,或者使用一些可以连接到 Maven 设置的外部工具?
PS 我基本上正在寻找像 Ruby 的 FakeWeb 这样的东西......
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您需要一个模拟框架来消除与 Web 相关的调用(并适当地对您的软件进行分层以插入真正的 Web 框架或模拟的等效框架)。 JMock 和 Mockito 都工作得很好。
您需要将它们与测试框架结合使用,例如 Junit 或 TestNG。我已经将所有这些与 Scala 一起使用,并且它们按预期工作。
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.
您可以使用 Mockito 或其他模拟框架来模拟您的数据服务(实际执行调用的服务)(请参阅此处的示例:http://guilhebl.github.io/scala/backend/testing/2017/07/12/scala-play -test-mock-guice-inject/ )或者您可以使用来自对 Web 服务的调用结果的虚假结果来对路由器进行存根:
https://www.playframework.com/documentation/2.6.x/ScalaTestingWebServiceClients
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