2016-03-01 46 views
1

我已经实现并部署了一个JAX-RS服务,它现在已经启动并正在运行。我创建了一个具有测试服务方法(POST,GET,POST)的ONE方法的类,问题在于我不知道如何在Eclipse中运行此测试。运行JUnit测试方法与JAX-RS服务进行交互 - NoClassDefFoundError

测试类: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(); 
    } 
} 

这里是例外,当我运行:Eclipse的运行方式 - > JUnit测试

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的启动和运行并且应用程序(项目)成功发布。我只是不知道如何运行测试方法。我试图把方法放在main(String [] args)中,并删除@Test,但它仍然给予异常。我需要一种方法来运行该方法来测试服务。

注意:此应用程序来自第3章使用JAX-RS 2.0的RESTful Java,第2版,我已成功运行使用maven(无IDE,只是cmd)的book方法进行测试。但我想从Eclipse中测试这个。谢谢。

+0

请参阅http://stackoverflow.com/a/18334053/346899 – s106mo

回答

0

问题是Eclipse没有从Glassfish导入这个jar glassfish4.1.1 \ glassfish \ modules \ jsonp-jaxrs.jar所以我添加它并重新运行测试并且它正在工作。