Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 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 Reply

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"
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...