创建模拟服务器以测试RESTTEMPLATE的结果

发布于 2025-02-03 07:12:35 字数 1065 浏览 3 评论 0原文

我不确定是否可以编写可以模拟“ http:// localhost:8888/setup”站点的测试用例,因此上述代码可以击中它,我想检查“ http:// http:// local -host :8888/设置“正确接收了输入流。

InputStream inputStream = //got the inputStream;

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);

restTemplate.setRequestFactory(requestFactory);

InputStreamResource inputStreamResource = new InputStreamResource(inputStream){
    @Override
    public String getFilename(){
        return filename;
    }
    
    @Override
    public long contentLength(){
        return -1;
    }
}

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>():
body.add("file", inputStreamResource);

HttpHeader headers = new HttpHeader();
headers.setContentType(MediaType.MULTIPART_FORM_DATA)LinkedMultiValueMap

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

String url = "http://localhost:8888/setup";

restTemplate.postForObject(url, requestEntity, String.class);

I am not sure if it is possible to write a Test case that can mock the "http://localhost:8888/setup" site, so the above code can hit it and I want to check if the "http://localhost:8888/setup" received the inputStream correctly.

InputStream inputStream = //got the inputStream;

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);

restTemplate.setRequestFactory(requestFactory);

InputStreamResource inputStreamResource = new InputStreamResource(inputStream){
    @Override
    public String getFilename(){
        return filename;
    }
    
    @Override
    public long contentLength(){
        return -1;
    }
}

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>():
body.add("file", inputStreamResource);

HttpHeader headers = new HttpHeader();
headers.setContentType(MediaType.MULTIPART_FORM_DATA)LinkedMultiValueMap

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

String url = "http://localhost:8888/setup";

restTemplate.postForObject(url, requestEntity, String.class);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-02-10 07:12:35

尝试使用WireMock!

使用它的许多方法,然后当我使用它时,我曾经运行一个JAR(Wiremock Jar),并使用指定的端口在您的Localhost上产生了一个程序。从此以后,您可以通过在港口上击中Localhost来测试!

参考请查看:

htttps:/ /www.softwaretestinghelp.com/wiremock-tutorial/

​>

Try using Wiremock!

Many ways of using it, back then when I used it, I used to run a JAR (wiremock jar) and it spawns up a program on your localhost with your port specified. Henceforth, you can test by hitting that localhost on the port it's up!

For reference check this out :

https://www.softwaretestinghelp.com/wiremock-tutorial/

https://www.baeldung.com/introduction-to-wiremock

https://github.com/wiremock/wiremock

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文