2014-01-29 71 views
0

我有這個類JerseySpringSuiteTest.javajava.lang.AssertionError:預期:<200>卻被:<404>在球衣RESTful服務單元測試

@RunWith(Suite.class) 
@Suite.SuiteClasses({ ProductTest.class }) 
public class JerseySpringSuiteTest { 
    public static JerseyTest jerseyTest; 

    @BeforeClass 
    public static void init() { 
     jerseyTest = new JerseyTest(new WebAppDescriptor.Builder("com.product.resource") 
       .contextParam("contextConfigLocation", "classpath:applicationContext.xml").servletClass(SpringServlet.class) 
       .contextListenerClass(ContextLoaderListener.class).requestListenerClass(RequestContextListener.class).build()) { 

     }; 
    } 
} 

ProductTest.java

public class ProductTest{ 

    @Before 
    public void before() throws Exception { 
     JerseySpringSuiteTest.jerseyTest.setUp(); 
     ExecutorService productExecutorService = ContextLoaderListener.getCurrentWebApplicationContext().getBean(
       "productExecutorService", ExecutorService.class); 
     Assert.assertNotNull(productExecutorService); 
    } 

    @Test 
    public void testNoAction() { 
     WebResource webResource = JerseySpringSuiteTest.jerseyTest.resource(); 
     String responseMsg = webResource.path("product/").get(String.class); 
     Assert.assertEquals("No Action Specified", responseMsg); 
    } 

    @Test 
    public void testGetProducts() throws URISyntaxException { 
     WebResource webResource = JerseySpringSuiteTest.jerseyTest.resource().path(
       "Products/rest/product/getProducts/1/productName/01-01-2013/"); 
     ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 

     Assert.assertEquals(200, response.getStatus()); 
    } 
} 

and Product.java:

@Component 
@Path("/product") 
public class Product{ 
    @Autowired 
    private ProductService productService; 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public Object noAction() { 
     return "No Action Specified"; 
    } 

    @GET 
    @Path("/getProducts/{companyID}/{companyName}/{date}") 
    @Produces(MediaType.APPLICATION_JSON) 
    public Object getProducts(@PathParam("companyID") final int companyID, 
      @PathParam("date") final String date, @PathParam("companyName") final String companyName) 
      throws IOException { 
     return productService.getProducts(companyID, companyName, date); 
    } 
} 

當我執行它,我看到:

java.lang.AssertionError: expected:<200> but was:<404> 
    at org.junit.Assert.fail(Assert.java:88) 
    at org.junit.Assert.failNotEquals(Assert.java:743) 
    at org.junit.Assert.assertEquals(Assert.java:118) 
    at org.junit.Assert.assertEquals(Assert.java:555) 
    at org.junit.Assert.assertEquals(Assert.java:542) 
    at com.product.resource.MonitoringEngineResourceTest.testGetProducts(ProductTest.java:27) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.junit.runners.Suite.runChild(Suite.java:127) 
    at org.junit.runners.Suite.runChild(Suite.java:26) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我能夠得到的數據在瀏覽器中,當我平安URL。

有人可以幫我獲得200和JSON對象的響應狀態嗎?

注意:我使用球衣1.17.1版和grizzly2容器。

+0

404意味着服務返回[*「Not Found」*](http://en.wikipedia.org/wiki/HTTP_404) - 也許資源的URL是錯誤的? – user2864740

回答

1

根據您的noAction單元測試,正確的URL似乎是:

webResource.path("product/") 

這一點也得到實際的服務類證實:

@Path("/product") 
public class Product { 
    //... 
} 

這意味着你需要改變你的其他測試使用相同的路徑。

此外,它是companyName,而不是productName根據您的服務等級。

@Test 
public void testGetProducts() throws URISyntaxException { 

    WebResource webResource = JerseySpringSuiteTest.jerseyTest.resource(); 
    ClientResponse response = 
     webResource.path("product/getProducts/1/companyName/01-01-2013/") 
        .accept(MediaType.APPLICATION_JSON) 
        .get(ClientResponse.class); 

    Assert.assertEquals(200, response.getStatus()); 
} 

一個可能的原因是它在瀏覽器是您的服務器(Tomcat的,碼頭等)配置爲通用網址的前綴Products/rest添加到所有路徑。執行單元測試時,此服務器不在圖片中。

相關問題