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

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

问题描述

我是单位测试的新手,我想在项目中测试一些球衣服务。我们正在使用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< Product> )。我想编写测试用例来检查响应状态和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测试框架(已在其他答案中提到 - 请参见此处1.17版的文档: https://jersey.java.net/documentation/1.17/test-framework.html )和REST-Assured( https://code.google.com/p/rest-assured ) - 请在此处查看两者的比较/设置(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更多inter esting和强大,但泽西测试框架也很容易使用。在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 /休息保证的/维基/使用)。另一个很好的教程就是 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天全站免登陆