gpt4 book ai didi

java - 运行 junit 测试方法以与 JAX-RS 服务交互 - NoClassDefFoundError

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:01 25 4
gpt4 key购买 nike

我已经实现并部署了 JAX-RS 服务,它现在已启动并正在运行。我创建了一个类,它有一个方法来测试服务方法(POST、GET、POST),问题是我不知道如何在 Eclipse 中运行此测试。

测试类:JAX-RS 2.0 客户端

public class CustomerResourceTest {
@Test
public void testCustomerResource() throws Exception {
Client client = ClientBuilder.newClient();
try {
System.out.println("*** Create a new Customer ***");

String xml = "<customer>" + "<first-name>Bill</first-name>" + "<last-name>Burke</last-name>"
+ "<street>256 Clarendon Street</street>" + "<city>Boston</city>" + "<state>MA</state>"
+ "<zip>02115</zip>" + "<country>USA</country>" + "</customer>";

Response response = client.target("http://localhost:8080/jax-rs/services/customers").request()
.post(Entity.xml(xml));
if (response.getStatus() != 201)
throw new RuntimeException("Failed to create");
String location = response.getLocation().toString();
System.out.println("Location: " + location);
response.close();

System.out.println("*** GET Created Customer **");
String customer = client.target(location).request().get(String.class);
System.out.println(customer);

String updateCustomer = "<customer>" + "<first-name>William</first-name>" + "<last-name>Burke</last-name>"
+ "<street>256 Clarendon Street</street>" + "<city>Boston</city>" + "<state>MA</state>"
+ "<zip>02115</zip>" + "<country>USA</country>" + "</customer>";
response = client.target(location).request().put(Entity.xml(updateCustomer));
if (response.getStatus() != 204)
throw new RuntimeException("Failed to update");
response.close();
System.out.println("**** After Update ***");
customer = client.target(location).request().get(String.class);
System.out.println(customer);
} finally {
client.close();
}
}

当我运行时,这是一个异常(exception):Eclipse run as -> Junit test

java.lang.NoClassDefFoundError: org/glassfish/json/jaxrs/JsonStructureBodyReader
at org.glassfish.jersey.jsonp.JsonProcessingFeature.configure(JsonProcessingFeature.java:68)
at org.glassfish.jersey.model.internal.CommonConfig.configureFeatures(CommonConfig.java:714)
at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:644)
at org.glassfish.jersey.client.ClientConfig$State.configureMetaProviders(ClientConfig.java:372)
at org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:405)
at org.glassfish.jersey.client.ClientConfig$State.access$000(ClientConfig.java:90)
at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:122)
at org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:119)
at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:340)
at org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:733)
at org.glassfish.jersey.client.ClientRequest.getConfiguration(ClientRequest.java:285)
at org.glassfish.jersey.client.JerseyInvocation.validateHttpMethodAndEntity(JerseyInvocation.java:135)
at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:105)
at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:101)
at org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at ch3.pt1.test.CustomerResourceTest.testCustomerResource(CustomerResourceTest.java:26)

正如我所说,Glassfish 已启动并正在运行,并且应用程序(项目)已成功 deolpoyed。我只是不知道如何运行测试方法。我尝试将该方法放入 main(String[] args) 中并删除 @Test 但它仍然给出异常。我需要一种方法来运行该方法来测试服务。

注意:这个应用程序来自RESTful Java with JAX-RS 2.0, 2nd Edition中的第3章,我已经使用maven(没有IDE,只有cmd)的书本方法成功运行了测试。但我想从 Eclipse 测试一下。谢谢。

最佳答案

请阅读https://jersey.java.net/documentation/latest/test-framework.html 。您需要继承JerseyTest,并指定您要使用的容器。

关于java - 运行 junit 测试方法以与 JAX-RS 服务交互 - NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728788/

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