【问题标题】:Mocking / stubbing "third party" services during integrated development在集成开发期间模拟/存根“第三方”服务
【发布时间】:2013-11-20 01:57:06
【问题描述】:

我目前正在使用的应用程序正在由 3 个独立的团队开发,每个团队在一天结束时共同处理不同的功能领域。困难在于使 3 个团队始终保持同步,并且不让一个团队的问题影响另一个团队。我正在寻找一种方法来存根/模拟对其他团队提供的其中一些服务的调用,以便我们可以在大部分时间单独工作,但在需要时快速切换回集成模式。

理想情况下我希望:

- during normal development, I could turn on a flag and those services will be mock services (for example, when I am just developing away on my part of the code and don't really care if the other team's service returns the right thing, just that it returns something)
- I don't want to have add code to check this flag everywhere in the code and if it is on, use the mock, else use the real thing... I just want it to automatically know to use the mock class when this flag is on

我们正在使用 Java 7 + CDI + Jboss。这可能与某种接线或过滤器有关吗?

TIA。

【问题讨论】:

标签: java integration jboss7.x cdi stubbing


【解决方案1】:

使用 Alternatives 可以更好地实现这一点,您可以在需要时通过 beans.xml 中的 disable Alternative 切换回集成模式

CDI Alternatives 非常适合 Mock 服务。

但是,您无需更改应用程序的源代码,而是可以在部署时使用替代方案进行选择。

替代品通常用于以下目的:

To handle client-specific business logic that is determined at runtime

To specify beans that are valid for a particular deployment scenario
   (for example, when country-specific sales tax laws require 
   country-specific sales tax business logic)

To create dummy (mock) versions of beans to be used for testing

替代方案允许您在执行时使用 beans.xml 文件覆盖它 - 一个简单的部署工件。

一个典型的场景是为不同的环境使用不同的 beans.xml,从而为您不想在本地/集成环境中执行的组件启用模拟替代方案。

Using Alternatives in CDI Applications

【讨论】:

  • 感谢您的建议,我会考虑使用这种方法。您有使用 CDI Extensions 与 CDI Alternatives 的经验吗?
【解决方案2】:

在基于 Spring 的应用程序(使用依赖注入)中,我通过保留两个应用程序上下文来实现这一点,一个配置为使用存根,一个配置为使用真实代码。我编写了一些包装器代码以在正确的时间加载适当的应用程序上下文,但您可以通过其他方式管理它,即使用使用不同类路径的单独运行目标。

就存根而言,我经常手动创建它们(即,编写包含固定数据电子表格或生成随机数据的存根服务),但 Mockito 在构建某些类型的存根时可能很有用。

根据您需要存根的资源类型(以及您是否有预算),另一种选择是服务虚拟化。我没有使用服务虚拟化工具的任何直接经验,但我当前的客户正在使用商业的LISA 工具来存根SOA 服务层。我推测有几家公司销售类似的工具。

【讨论】:

  • 感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 2014-10-09
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 2018-12-30
  • 2015-05-15
  • 2017-12-27
相关资源
最近更新 更多