java.lang.AssertionError:expected:< 200>但是:< 404>在球衣休息服务单元测试

时间:2014-01-29 04:32:20

标签: java json unit-testing rest junit

我有这个课 JerseySpringSuiteTest.java

@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());
    }
}

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)

我在浏览器中ping网址时能够获取数据。

有人可以帮助我获得200和JSON对象的响应状态吗?

注意:我正在使用jersey 1.17.1版本和grizzly2容器。

1 个答案:

答案 0 :(得分:1)

根据您的noAction单元测试,正确的网址似乎是:

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,Jetty等)配置为将公共URL前缀Products/rest添加到所有路径。执行单元测试时,此服务器不在图片中。