[英]What is the best way to unit test REST Endpoints (Jersey)
I have a REST controller that has multiple GET/POST/PUT methods that all respond/request JSON. 我有一个REST控制器,它有多个GET / POST / PUT方法,都响应/请求JSON。 I am not using Spring in this application (yet).
我还没有在这个应用程序中使用Spring。
I was looking into the REST-assured framework and I like how that looks but I can only use it when my web server is up and running. 我正在研究REST保证的框架,我喜欢它看起来如何,但我只能在我的Web服务器启动并运行时使用它。
Is there a way for me to run a in-memory web server, or something like that? 有没有办法让我运行内存中的Web服务器,或类似的东西? Are there any examples of REST endpoint testing that someone can provide?
是否有人可以提供的REST端点测试示例?
If you are using JAX-RS 2.0 you should find your answer here 如果您使用的是JAX-RS 2.0,您应该在这里找到答案
You can take a look at the example also 你也可以看看这个例子
An integration test example, could be: 集成测试示例可以是:
public class CustomerRestServiceIT {
@Test
public void shouldCheckURIs() throws IOException {
URI uri = UriBuilder.fromUri("http://localhost/").port(8282).build();
// Create an HTTP server listening at port 8282
HttpServer server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0);
// Create a handler wrapping the JAX-RS application
HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new ApplicationConfig(), HttpHandler.class);
// Map JAX-RS handler to the server root
server.createContext(uri.getPath(), handler);
// Start the server
server.start();
Client client = ClientFactory.newClient();
// Valid URIs
assertEquals(200, client.target("http://localhost:8282/customer/agoncal").request().get().getStatus());
assertEquals(200, client.target("http://localhost:8282/customer/1234").request().get().getStatus());
assertEquals(200, client.target("http://localhost:8282/customer?zip=75012").request().get().getStatus());
assertEquals(200, client.target("http://localhost:8282/customer/search;firstname=John;surname=Smith").request().get().getStatus());
// Invalid URIs
assertEquals(404, client.target("http://localhost:8282/customer/AGONCAL").request().get().getStatus());
assertEquals(404, client.target("http://localhost:8282/customer/dummy/1234").request().get().getStatus());
// Stop HTTP server
server.stop(0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.