1

I have a main feature file which brings up the mock and call an API. This main API will call other API in the background which goes to my karate mock server. I am able to validate incoming request and send out a response as well. In this incoming request on mock, I receive a transactionRef, which then appears in the response of my main API.

The flow is something like this

API1 Request 
     API2 Request (transactionRef) -> API2 Response 
API1 Response (transactionRef)

What I want to check is these both are same. Is that possible? I tried setting a variable in mock feature but not able to access it in main feature.

Feature:  stateful mock server

Background: Mock

Scenario: pathMatches('/abcd') && bodyPath('/Envelope/Body/infoRequest')!= null
    
    * match karate.typeOf(request) == 'xml'
      
    * xml respInfo = read('classpath:org/xbspro/hos/payloads/infoResponse.xml')
    * def tranRef = $request/Envelope/Body/infoRequest/transactionId

    * set respInfo/Envelope/Body/infoResponse/transactionId= tranRef 
 
    * def response = respInfo

Above is my mock feature. Below is my main feature

    * call read('classpath:Abstract.feature')
    * xml remResp = response
    
    * match remResponse //transactionId == tranRef 
shrik18
  • 107
  • 4

1 Answers1

1

You have 2 options.

  1. You already know how to capture state in the mock by using global variables. Just add a custom end-point which you can call from your API test to get it. Refer: https://stackoverflow.com/a/63512034/143475

  2. Refer to this example where you can use Java to "connect" a mock and the test: https://twitter.com/getkarate/status/1417023536082812935

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248