mock https request in java

2020-07-11 08:19发布

Let's say I'm writing an application and I need to be able to do something like this:

String url = "https://someurl/";
GetMethod method = new GetMethod(URLEncoder.encode(url));
String content = method.getResponseBodyAsString();

Is there a way to provide a mock server that would let me handle the https request? What I'm looking for is a way to write unit tests, but I need to be able to mock the part that actually goes out to https://someurl so I can get a known response back.

8条回答
再贱就再见
2楼-- · 2020-07-11 08:48

You could always launch a thttpd server as part of your unit test to serve the requests locally. Though, ideally, you have a well tested GetMethod, and then you can just mock it, and not have to actually have a remote server around for ALL of your tests.

Resources

查看更多
Anthone
3楼-- · 2020-07-11 08:51

Use xml mimic stub server, that can simulate static http response based on request parameters, headers, etc. It is very simple to configure and use it.

http://xmlmimic.sourceforge.net/ http://sourceforge.net/projects/xmlmimic/

查看更多
登录 后发表回答