单元测试球衣 Restful Services [英] Unit testing jersey Restful Services

查看:19
本文介绍了单元测试球衣 Restful Services的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试的新手,我想在一个项目中测试一些 jersey 服务.我们正在使用 Junit.请指导我以更好的方式编写测试用例.

I'm new to unit testing and I want to test some jersey services in a project. We are using Junit. Please guide me to write test cases in better way.

代码:

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

上述服务工作正常,我想编写junit测试用例来测试上述方法.上述方法将检索 JSON 格式的产品列表(List).我想编写测试用例来检查响应状态和 json 格式.

Above mentioned service is working fine and I want to write junit test case to test above mentioned method. Above method will retrieve list of products (List<Product>) in JSON format. I would like to write test case to check response status and json format.

注意:我们使用的是 Jersey 1.17.1 版本.

NOTE: We are using Jersey 1.17.1 version.

将不胜感激:)

推荐答案

对于 Jersey 网络服务测试,有几个测试框架,即: Jersey 测试框架(已在其他答案中提到 - 请参阅此处的 1.17 版文档:https://jersey.java.net/documentation/1.17/test-framework.html) 和 REST-Assured (https://code.google.com/p/rest-assured) - 在这里查看比较/setup (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/).

For Jersey web services testing there are several testing frameworks, namely: Jersey Test Framework (already mentioned in other answer - see here documentation for version 1.17 here: https://jersey.java.net/documentation/1.17/test-framework.html) and REST-Assured (https://code.google.com/p/rest-assured) - see here a comparison/setup of both (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/).

我发现 REST-Assured 更有趣、更强大,但 Jersey 测试框架也非常易于使用.在 REST-Assured 中编写测试用例检查响应状态和 json 格式",您可以编写以下测试(非常类似于您在 jUnit 中所做的):

I find the REST-Assured more interesting and powerful, but Jersey Test Framework is very easy to use too. In REST-Assured to write a test case "to check response status and json format" you could write the following test (very much like you do in jUnit):

package com.example.rest;

import static com.jayway.restassured.RestAssured.expect;
import groovyx.net.http.ContentType;

import org.junit.Before;
import org.junit.Test;

import com.jayway.restassured.RestAssured;

public class Products{

    @Before
    public void setUp(){
        RestAssured.basePath = "http://localhost:8080";
    }

    @Test
    public void testGetProducts(){
        expect().statusCode(200).contentType(ContentType.JSON).when()
                .get("/getProducts/companyid/companyname/12345088723");
    }

}

这应该对您有用...您也可以非常轻松地验证 JSON 特定元素和许多其他细节.有关更多功能的说明,您可以查看 REST-Assured (https://code.google.com/p/rest-assured/wiki/Usage).另一个很好的教程是这个 http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/.

This should do the trick for you... you can verify JSON specific elements also very easily and many other details. For instructions on more features you can check the very good guide from REST-Assured (https://code.google.com/p/rest-assured/wiki/Usage). Another good tutorial is this one http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/.

HTH.

这篇关于单元测试球衣 Restful Services的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆