I am using Karate to do integration testing for a Spring Boot app. The app consumes two other services (Service A and Service B) . I have written Karate mocks for Service A and B. My integration tests are written as two different feature files. One of the feature is tested using Mocks for service A and B. Another feature uses Mocks for service B and Spring Contract Stubs for service A.
Feature with only mocks
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})
Feature with mocks and Spring Contract stubs
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})
Now, when we run the tests, the second one fails saying that the port is already used. I tried using karate.stop(9000)
at the end of the feature file which is running first, but it does not help. Also, I am not sure about the behaviour of this stop method. Any suggestions to solve this problem ? Why the mock service is still running even after the tests are done ?
As far as I know, the mock should stop when the JVM exits - so I can't explain what's going on in your case. So maybe you should create a way to replicate and file a bug: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
I personally recommend starting your mocks from the unit-test Java code (typically the JUnit class) which means you can keep a reference to the mock and then call stop()
on it. Even here, normally it is not mandatory, as the mock should terminate along with the JVM. Read the docs here: https://github.com/intuit/karate/tree/develop/karate-netty#embedding
Then, note that best-practice is to dynamically provision a port and then pass that to the test or any other consumers. Especially in CI-CD pipelines, this avoids the problem of the port in use or taking too much time to de-allocate.
And maybe the direct answer you are looking for. When you call karate.start()
you get an object back which is of type MockServer
. So you can keep a reference to it and call stop()
on it when needed: https://github.com/intuit/karate/tree/develop/karate-netty#within-a-karate-test
For example:
* def server1 = karate.start('mock1.feature')
* def port1 = server1.port
# do some tests
* server1.stop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.