gpt4 book ai didi

java - 使用 JUnit 和 Jersey Client 测试 JAX-RS 应用程序

转载 作者:行者123 更新时间:2023-12-01 08:49:08 26 4
gpt4 key购买 nike

我有一个在 WildFly 10 上运行的 Java EE 应用程序。该应用程序使用 Jersey,并且当我使用 REST 客户端对其进行测试时运行良好。

我编写了一个 JUnit 测试,该测试使用 Jersey Client API 向上述应用程序发出请求。当我运行它时,我得到以下信息:

javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:209)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:174)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:473)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:165)

跟踪中的下一行引用下面的 request() 调用:

@Test
public void test() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/myapp/webapi");
target.path("/users");
String response = target.request("application/json").get(String.class);
assertEquals("test", response);
}

有什么想法吗?

最佳答案

您的问题来自服务器端(参见错误500),如果您想自己检查,请打开浏览器并访问url:http://localhost:8080/myapp/webapi

还请注意 Javadoc WebTarget.path() 返回一个新的 WebTarget

https://jersey.java.net/apidocs/2.22/jersey/javax/ws/rs/client/WebTarget.html#path(java.lang.String)

我相信你的代码你真正想做的是:

@Test
public void test() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/myapp/webapi");
WebTarget targetUpdated = target.path("/users");
String response = targetUpdated.request("application/json").get(String.class);
assertEquals("test", response);
}

关于java - 使用 JUnit 和 Jersey Client 测试 JAX-RS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42494137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com