assured以及json路径的java-cucumber数据表

wmvff8tz  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(396)

我在小 cucumber 中有一个类似的步骤:

And the date range options are correct
      | startDate  | endDate    | isDefault | isCurrent |
      | 2020-03-02 | 2021-03-01 | false     | false     |
      | 2019-03-02 | 2020-03-01 | false     | false     |

在steps definition类中,我尝试使用rest assured和hamcrest来验证json响应中的日期范围。

@And("the date range options are correct")
    public void validateDateRanges(DataTable expectedDateRanges) {
        String response = getJSONResponse();
        List<Map<String,Object>> expected = expectedDateRanges.asMaps(String.class, Object.class);
        assertThat("Date Ranges are correct", from(response).get("dateRangeOptions"), equalTo(expected));
    }

但是,此操作失败,并出现以下错误:

Expected: <[{startDate=2020-03-02, endDate=2021-03-01, isDefault=false, isCurrent=false}, {startDate=2019-03-02, endDate=2020-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2019-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2018-06-17, isDefault=true, isCurrent=false}, {startDate=2017-03-02, endDate=2018-03-01, isDefault=false, isCurrent=false}, {startDate=2016-03-02, endDate=2017-03-01, isDefault=false, isCurrent=false}, {startDate=2015-08-18, endDate=2016-03-01, isDefault=false, isCurrent=false}]>
     but: was <[{startDate=2020-03-02, endDate=2021-03-01, isDefault=false, isCurrent=false}, {startDate=2019-03-02, endDate=2020-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2019-03-01, isDefault=false, isCurrent=false}, {startDate=2018-03-02, endDate=2018-06-17, isDefault=true, isCurrent=false}, {startDate=2017-03-02, endDate=2018-03-01, isDefault=false, isCurrent=false}, {startDate=2016-03-02, endDate=2017-03-01, isDefault=false, isCurrent=false}, {startDate=2015-08-18, endDate=2016-03-01, isDefault=false, isCurrent=false}]>
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at scenarios.pagination.AccrualLog.validateDateRanges(AccrualLog.java:80)
    at ✽.the date range options are correct(file:///.../accrualLog.feature:7)

考虑到这些值看起来都是正确的,我认为这是由于数据类型不匹配造成的-布尔字段作为字符串从datatable返回。
除了使用类来表示日期范围之外,还有什么方法可以解决这个问题吗?

dldeef67

dldeef671#

我找到了解决办法。最后,我创建了自己的matcher实现来检查cucumber数据表:

public class DataTableMatcher extends BaseMatcher<Object> {

    private final List<Map<String,String>> expectedValues;

    public DataTableMatcher(Object expectedValue) {
        this.expectedValues = ((DataTable) expectedValue).asMaps();
    }

    public boolean matches(Object o) {
        List<?> actual = (List<?>) o;
        List<Map<String,String>> actualValues = actual.stream().map(this::castRow).collect(Collectors.toList());
        return expectedValues.equals(actualValues);
    }

    private Map<String,String> castRow(Object row) {
        Map<String,String> result = new HashMap<>();
        Map<?,?> cast = (Map<?,?>) row;
        for (Object column : cast.keySet()) {
            result.put(column.toString(), cast.get(column).toString());
        }
        return result;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("It to be right"); //TODO more helpful error message
    }

    public static Matcher<Object> dataTablesMatch(Object t) {
        return new DataTableMatcher(t);
    }
}

我可以这样使用它:

@And("the date range options are correct")
    public void validateDateRanges(DataTable expectedDateRanges) {
        String response = getJSONResponse();
        assertThat("Date Ranges are correct", from(response).get("dateRangeOptions"), dataTablesMatch(expectedDateRanges));
    }

这很管用。不过,我很想知道是否有更优雅的解决方案。

相关问题