有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何为类编写JUnit测试用例

我是一名新生,我需要知道如何为这个类编写JUnit测试用例。有人能帮我吗

package com.jpmc.cb.creos.util.grid;

public class GridHelper {

    public static List<GridFilter> getGridFilters(String jsonFilters)throws JsonParseException, JsonMappingException, IOException
    {
        List<GridFilter> filters = new ArrayList<GridFilter>();
        GridFilter filter[] = new ObjectMapper().readValue(jsonFilters,
                GridFilter[].class);
        for (int i = 0; i < filter.length; i++) {
            filters.add(filter[i]);
        }
        return filters;
    }
}

这是GridFilter类:

package com.jpmc.cb.creos.util.grid;

public class GridFilter {

private String property;
private String value;
private String operator;

public GridFilter() {}

public GridFilter(String property, String operator, String value)
{
    this.property = property;
    this.value = value;
    this.operator = operator;
}

public String getProperty() {
    return property;
}

public void setProperty(String property) {
    this.property = property;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public String getOperator() {
    return operator;
}

public void setOperator(String operator) {
    this.operator = operator;
}

}

共 (1) 个答案

  1. # 1 楼答案

    下面是一些关于测试内容的想法。编写(至少)以下情况的测试方法:

    • 具有2个过滤器的格式良好的json应该返回一个列表,其中包含两个具有匹配值的过滤器
    • 没有过滤器的格式良好的json应该返回空列表
    • 格式错误的json应引发JsonParseException
    • 引发其他异常类型的其他坏数据

    如何编写测试用例?不清楚你在问什么。但这里有一个例子:

    @Test
    public void empty_json_gives_empty_list() throws Exception {
        assertEquals(Collections.emptyList(), GridHelper.getGridFilters("[]"));
    }