Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

java - Mock/not-mock response on a per request basis in Integration test

I am writing Integration tests. I have a need where for a positive test case request hit an actual service and recieve the response. But for a negative test case I must get the mocked response.

I am curious to understand if there is a way that I can mock/not-mock the request on a per configuration basis. Like for example if request accepts email address in request and I provide

  1. "[email protected]" - response from mockoon must be a mocked response.
  2. "[email protected]" - mocking must not happen but rather it must hit the actual server to get the response. may be via redirecting or calling the actual service and responding the response to the caller.

I have tried Mockoon but feature is not yet present. So trying to help from the community :)

Regards,

question from:https://stackoverflow.com/questions/66063347/mock-not-mock-response-on-a-per-request-basis-in-integration-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use separate stub/mappings that match on different emails. Assuming that your url is some-Url and uses a queryParameter of email...

{
    "request": {
        "url": "/some-Url",
        "queryParameters": {
            "email": {
                "equalTo": "[email protected]"
            }
        }
    },
    "response": {
        "status": 200
    }
}
{
    "request": {
        "url": "/some-Url",
        "queryParameters": {
            "email": {
                "equalTo": "[email protected]"
            }
        }
    },
    "response": {
        "proxyBaseUrl": "http://my-other-url.com"
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...