在应用程序-go + BDD-java中模拟第三方服务

huangapple go评论97阅读模式
英文:

Mocking third party service in application-go + BDD-java

问题

最近我开始研究使用Gherkin和Restassured的BDD。需要模拟第三方服务,以下是我的用例。

  1. Service-A内部调用Service-B。
  2. 应用程序使用goLang编写。
  3. BDD使用Java编写。

我们有一个正在运行的CI流水线,它生成rpm并将rpm部署到虚拟机中。
在该虚拟机上,我们正在运行BDD(目前Service-A和Service-B都部署在同一台虚拟机上)。

是否有一种方法可以模拟Service-B,以便我不必依赖于Service-B?如果可以,这里最好的方法是什么。

我已经尝试使用goLang的httptest在单元测试级别模拟服务。
但是在流水线中创建了rpm并使用BDD后,如何进行模拟。

谢谢。

英文:

I recently started looking into BDD(using Gherkin + Restassured). Need to mock third party servicd, below is my use case.

  1. Service-A internally calls Service-B
  2. The application is in goLang.
  3. The BDD are in Java.

We have a CI pipeline running along, where it generates the rpm and deploys the rpm into VM.
On that VM we are running the BDD(Currently Service-A and Service-B are deployed on the same VM)

Is ther a way i can mock the Service-B, so that i dont have to be dependent on Service-B? If yes what would be the best approach here.

Have tried goLang httptest to mock the service at the unit-test level.
But how the mocking can be done after rpm gets created in pipeline with BDD in place.

Thanks

答案1

得分: 1

如果您的服务A在内部调用服务B,而不是通过网络或RPC调用,那么您可以使用依赖注入来注入一个“假”的服务B版本。(请注意,这不一定涉及依赖注入框架;基于构造函数和基于属性的注入也是有效的)。如果服务B没有接口,可以提取一个接口,并使用一个薄适配器来调用真实的服务或根据环境调用假服务。

只要您的场景只与服务A的用户界面或API进行交互,您就不需要更改这些场景。

您需要更改构建流水线的工作方式,以便它部署您的假代码而不是真实代码。

您甚至可以在运行时进行此操作,通过使适配器调用相关服务,从假服务切换到真实服务。切换或部署可以由环境变量或构建参数触发。

但要小心不要将测试服务部署到生产环境中!

如果您正在使用持续部署,那么构建流水线中的最后一步应该是部署并测试与真实服务的交互。如果由于某种原因这是您唯一的工作方式,仍然有一些事情可以帮助您:

  • 您可以对服务B使用存根数据,以使其以可预测的方式运行。

  • 您可以使用测试实例。与您的服务提供商联系,看看他们是否为您提供了一个测试实例。我建议您仍然检查真实服务的部署是否成功,最好使用某种自动化测试进行检查,即使必须在生产环境中运行。它只需要是一个基本的冒烟测试,以检查系统是否正确连接。请注意,部署越容易,从任何错误中恢复就越容易;如果无法快速部署,则需要更加彻底地检查。

如果RPM包在没有任何假或测试实例的情况下创建和部署,并且您无法配置环境以使用此类假或测试实例,那么您将无法模拟它。构建流水线必须是部署假服务的一部分。如果您对CI流水线有控制权,这将不是问题;否则,请联系您的构建团队。他们可能有经验,或者能够指导您寻找其他可以帮助您的人。毕竟,优秀的BDD是由对话驱动的!

英文:

If your Service A is calling Service B internally, rather than via web or RPC, then you can use dependency injection to inject a "fake" version of your Service B. (Note that this doesn't necessarily involve a dependency injection framework; constructor-based and property-based injection are also valid). If Service B has no interface, extract one and use a thin adapter to call the real service or fake depending on environment.

You won't need to change your scenarios as long as they are only interacting with Service A's user interface or API.

You will need to change the way the build pipeline works, so that it deploys with your fake instead of the real code.

You can even do this at runtime, switching over from the fake to the real thing by having the adapter call the relevant service. The switch or deployment can be triggered by environment variables or by build arguments.

Be careful not to deploy your test service to production though!

If you're using continuous deployment, then the last step in the build pipeline should ideally deploy and test interaction with the real service. If for some reason that's the only way you can work, there are still a couple of things you can do that might help:

  • You can stub the data that Service B uses, so that it behaves in a predictable way

  • You can use a test instance. Reach out to your service provider and see if they have one for you. I recommend that you should still check that deployment of the real service succeeds, ideally with an automated test of some sort, even if that has to be run in production. It only needs to be a basic smoke test to check that the system is wired up. Note that the easier it is to deploy, the easier it will be to recover from any mistakes; if you can't deploy quickly then you will need to be more thorough in your checking.

If the RPM is created and deployed without any kind of fake or test instance, and you have no way to configure the environment to use such a fake or test instance, then you will not be able to mock it out. The build pipeline has to be a part of deploying a fake. That won't be a problem if you have control over your CI pipeline; otherwise reach out to your build team. They may have experience or be able to point you to someone else who can help you. Great BDD is driven by conversations, after all!

huangapple
  • 本文由 发表于 2023年2月10日 18:04:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75409545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定