我已经实施并部署了一个JAX-RS服务,它现在正在运行。我创建了一个类,它有一个方法来测试服务方法(POST,GET,POST),问题是我不知道如何在Eclipse中运行这个测试。
测试类:JAX-RS 2.0客户端
query = CityGroup.objects.all().order_by('city__region')
这是例外,当我运行时:Eclipse运行为 - > Junit测试
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();
}
}
正如我所说,Glassfish已启动并正在运行,应用程序(项目)已成功发布。我只是不知道如何运行测试方法。我试图将方法放在main(String [] args)中并删除@Test但它仍然给出ann异常。我需要一种方法来运行该方法来测试服务。
注意:此应用程序来自 RESTful Java with JAX-RS 2.0,2nd Edition 的第3章,我已经使用使用maven的book方法成功运行测试(没有IDE,只有cmd) 。但我想从Eclipse测试这个。感谢。
答案 0 :(得分:0)
请阅读https://jersey.java.net/documentation/latest/test-framework.html。您需要从JerseyTest继承,并指定要使用的容器。
答案 1 :(得分:0)
问题是Eclipse没有从Glassfish glassfish4.1.1 \ glassfish \ modules \ jsonp-jaxrs.jar 导入这个jar,所以我添加它并重新运行测试并且它&#39;工作。