【问题标题】:Stopping Karate mock server at the end of Integration testing在集成测试结束时停止空手道模拟服务器
【发布时间】:2021-07-21 12:44:14
【问题描述】:

我正在使用空手道对 Spring Boot 应用程序进行集成测试。该应用程序使用另外两个服务(服务 A 和服务 B)。我为服务 A 和 B 编写了空手道模拟。我的集成测试被编写为两个不同的功能文件。其中一个功能使用 Mocks 测试服务 A 和 B。另一个功能使用 Mocks 测试服务 B,使用 Spring Contract Stubs 测试服务 A。

只有模拟的功能

 Feature: Test Some feature

  Background:
    * configure headers = { Content-Type: 'application/json' }
    * url baseUrl

 #Start up the mocks
  Scenario: Start the Mocks
    * karate.start({ mock: '../mock/service/service-a.feature', port: 9000})
    * karate.start({ mock: '../mock/service/service-b.feature', port: 9001})

带有模拟和 Spring Contract 存根的功能

 Feature: Test Some more features

  Background:
    * configure headers = { Content-Type: 'application/json' }
    * url baseUrl

 #Start up the mocks
  Scenario: Start the Mocks
    # Service A will use Spring Contract stubrunner. 
    * karate.start({ mock: '../mock/service/service-b.feature', port: 9001})

现在,当我们运行测试时,第二个测试失败,说明端口已被使用。我尝试在首先运行的功能文件末尾使用karate.stop(9000),但这没有帮助。另外,我不确定此停止方法的行为。有什么建议可以解决这个问题吗?为什么测试完成后 mock 服务仍在运行?

【问题讨论】:

    标签: java spring-boot integration-testing karate


    【解决方案1】:

    据我所知,当 JVM 退出时,模拟应该停止 - 所以我无法解释你的情况。所以也许你应该创建一种方法来复制和提交错误:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

    我个人建议从单元测试 Java 代码(通常是 JUnit 类)开始模拟,这意味着您可以保留对模拟的引用,然后在其上调用 stop()。即使在这里,通常也不是强制性的,因为模拟应该与 JVM 一起终止。在此处阅读文档:https://github.com/intuit/karate/tree/develop/karate-netty#embedding

    然后,请注意最佳实践是动态配置端口,然后将其传递给测试或任何其他消费者。尤其是在 CI-CD 流水线中,这避免了端口在使用中或花费太多时间去分配的问题。

    也许是您正在寻找的直接答案。当你调用karate.start() 时,你会得到一个类型为MockServer 的对象。所以你可以保留对它的引用并在需要时调用stop()https://github.com/intuit/karate/tree/develop/karate-netty#within-a-karate-test

    例如:

    * def server1 = karate.start('mock1.feature')
    * def port1 = server1.port
    # do some tests
    * server1.stop()
    

    【讨论】:

    • 我试过server1.stop(),但它给了我一个错误,说 server1 没有定义。我设法通过从 JUnit 代码启动模拟并在执行测试后停止它来解决这个问题。谢谢。
    • @Jay 如果有“问题”,请提出错误。但很可能你错过了什么
    猜你喜欢
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多