[英]Stopping Karate mock server at the end of Integration testing
我正在使用 Karate 对 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)
,但没有帮助。 另外,我不确定这个 stop 方法的行为。 有什么建议可以解决这个问题吗? 为什么即使在测试完成后模拟服务仍在运行?
据我所知,当 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.