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

CODE:

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

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.

NOTE: We are using Jersey 1.17.1 version.

Help would be appreciated :)

推荐答案

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/).

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

}

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/.

嗯.

Json相关问答推荐

2020-12年草案中的Json列举模式

如何使用Laravel在MariaDB JSON kolumn中使用unicode字符

kotlinx-serialization:如何将具有不同类型对象的JsonArray转换为同一个Class

在PowerShell中,如何获取数据对所在的JSON对象的名称

Oracle plsql:如何将json文件加载到嵌套表中

如何将文件夹组织的.json文件合并为一个JSON文件,文件夹/文件名作为键

解析Ansible AWS ec2_security_group中的json以提取安全组ID

报告重复的对象键

如何使用jq按键 Select 并获取整个json输出来更改json中的多个值

shell解析json并循环输出组合变量

如何使用 Swiftui 判断 JSON 是否缺少键值对

解析包含换行符的 JSON

如何使用 Newtonsoft.Json 反序列化 JSON 数组

十六进制格式可以与 JSON 文件一起使用吗?如果是这样,怎么做?

如何在 django rest 框架中定义列表字段?

jQuery fullcalendar 发送自定义参数并使用 JSON 刷新日历

Jackson 动态属性名称

通过 JSON 发送 64 位值的公认方式是什么?

如何使用 Jackson 的 objectMapper 反序列化接口字段?

FastAPI:如何将正文读取为任何有效的 json?