我有一个像这样的对象
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ChartDataSearchCondition {
@NotNull
@NotEmpty
@Pattern(regexp = Constants.REGEX_CHART_CONDITION_DATE)
private String startDate;
@NotNull
@NotEmpty
@Pattern(regexp = Constants.REGEX_CHART_CONDITION_DATE)
private String endDate;
@NotNull @NotEmpty private List<String> columns;
}
这里是单元测试
public ChartDataSearchCondition createChartRequest(
String startDate, String endDate, List<String> columnList) {
ChartDataSearchCondition chartDataSearchCondition = new ChartDataSearchCondition();
chartDataSearchCondition.setColumns(columnList);
chartDataSearchCondition.setStartDate(startDate);
chartDataSearchCondition.setEndDate(endDate);
return chartDataSearchCondition;
}
@Test()
public void visualize_searchConditionWithEndDateIsNull() {
List<String> columns = new ArrayList<>();
columns.add("col2");
ChartDataSearchCondition chartDataSearchCondition =
createChartRequest("2021-06-01 19:00:00.000+0700", null, columns);
try {
controller.getTimeseriesSampling(cluster, database, container, chartDataSearchCondition);
Assert.fail();
} catch (CommonException e) {
assertThat(e.getCode(), is(ErrorCode.CHART_REQUEST_INVALID.getErrorCode()));
} catch (Exception e) {
Assert.fail();
}
}
但是当我运行我的UnitTest,有一个错误:
. lang。NullPointerException: endDate被标记为非空,但为空
我该怎么做才能解决这个问题?非常感谢
您在endDate
字段处向createChartRequest
传递了一个空值-
ChartDataSearchCondition chartDataSearchCondition = createChartRequest("2021-06-01 19:00:00.000+0700", "Add a timestamp value here", columns);