如何在Java中模拟Web服务器进行单元测试? [英] How to mock a web server for unit testing in Java?

查看:150
本文介绍了如何在Java中模拟Web服务器进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用模拟Web服务器创建单元测试。是否有一个用Java编写的Web服务器可以从JUnit测试用例中轻松启动和停止?

I would like to create a unit test using a mock web server. Is there a web server written in Java which can be easily started and stopped from a JUnit test case?

推荐答案

尝试简单 Maven )它非常容易嵌入单元测试中。参加RoundTripTest和例如 PostTest 。提供了如何将服务器嵌入测试用例的示例。

Try Simple(Maven) its very easy to embed in a unit test. Take the RoundTripTest and examples such as the PostTest written with Simple. Provides an example of how to embed the server into your test case.

此外,Simple比Jetty更轻,更快,没有依赖关系。因此,您不必在类路径中添加几个jar。您也不必关心 WEB-INF / web.xml 或任何其他工件。

Also Simple is much lighter and faster than Jetty, with no dependencies. So you won't have to add several jars on to your classpath. Nor will you have to be concerned with WEB-INF/web.xml or any other artifacts.

这篇关于如何在Java中模拟Web服务器进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆