Junit测试Map <string,list <string =“” >>

时间:2018-07-30 18:49:26

标签: java unit-testing exception junit mockito

我正在尝试对具有以下功能的类进行单元测试:

  private String getTabSeparateValues(final QueryParams params, final HttpServletRequest request) {
        MetricsSerializer serializer = new MetricsSerializer();
        return serializer.serializeValues(params, request);
    }

它在以下类中调用“ serializeValues(params,request)”函数:

public class MetricsSerializer {

    private final StringJoiner stringJoiner = new StringJoiner("\t");
    private static final String MONTH_FORMAT = "MMMMM";

      public String serializeMetrics(final QueryParams queryParams, final HttpServletRequest request) {
        addValueFromString(queryParams.getId());
        addValueFromString(getCurrentMonth());
        addValueFromString(request.getRemoteUser().split("@")[0]);
        addValueFromString(queryParams.getCurrency());
        addValuesFromList(queryParams.getCompanies());
        addValueFromString(queryParams.getCognosDatasetType());
        addValuesFromList(queryParams.getScenarios());
        addFilter(queryParams.getFilters());
        addGroupings(queryParams.getGroupings());
        addValueFromString(queryParams.getReportTemplate());
        return stringJoiner.toString();
    }

    private void addValueFromString(final String value) {
        stringJoiner.add(value);
    }

    private void addFilter(final List<Map<String, List<String>>> filters) {
        List<String> collect = filters.stream()
                .flatMap(entry -> entry.keySet()
                        .stream())
                .collect(Collectors.toList());
        addValuesFromList(collect);
    }

    private void addGroupings(final Map<String, List<String>> groupings) {
        addValuesFromList(new ArrayList<>(groupings.keySet()));
    }

    private void addValuesFromList(final List<String> listValues) {
        stringJoiner.add(listValues.stream().collect(Collectors.joining(" ")));
    }

    private String getCurrentMonth() {
        DateFormat monthFormat = new SimpleDateFormat(MONTH_FORMAT);
        return monthFormat.format(new Date());
    }

}

此类以制表符分隔的格式返回值。 QueryParams类的结构如下:

@Data
@Builder
public class QueryParams {
    private String datasetType;
    private Map<String, List<String>> groupings;
    private List<Aggregate> aggregates;
    private List<String> scenarios;
    private List<String> companies;
    private List<Map<String, List<String>>> filters;
    private List<NamedTimeRange> timeRanges;
    private Map<String, String> params;
    private String reportTemplate;
    private String id;
    private String currency;
}

我没有使用QueryParameters中的所有参数(跳过时间范围,参数,集合)。为了测试我是否真的得到制表符分隔的指标,我编写了以下测试:

public class MetricsHandlerTest {

    private QueryParams queryParams;

    @Before
    public void setUp() throws Exception {
        List<String> list = Arrays.asList("one", "two");
        List<String> scenarioList = Arrays.asList("A1");
    List<Map<String, List<String>>> filter = new ArrayList<>();
    Map<String, List<String>> filtersMap = new HashMap<>();
    List<String> filterList = Arrays.asList("COM");
    filtersMap.put("product", filterList);
    filter.add(filtersMap);
        queryParams = QueryParams.builder()
                .id("123").currency("USD").companies(list).scenarios(scenarioList).filters(filter)
                .build();
    }

    @Test
    public void tabSerializerTest() {
        String remoteUser = "testuser";
        HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
        Mockito.when(httpServletRequest.getRemoteUser())
                .thenReturn(remoteUser);
        MetricsSerializer metricsSerializer = new MetricsSerializer();
        String tabs = metricsSerializer.serializeMetrics(queryParams, httpServletRequest);
        assertEquals(tabs, "123 \t USD \t one two");
    }
}

我收到分组的空指针异常。 (在MetricsSerializer类的“ addGroupings”中)我尝试记录分组,并且它可以为null。对于方案和过滤器,我添加了值。如何处理可能为null的分组呢? 任何有关如何解决此问题的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我不确定是否理解您的问题。 MetricsSerializer 受您控制吗?如果是这样,则需要采取无效检查来处理分组为空的情况。

如果它不在您的控制范围之内,则需要像这样用空的Group(而不是null)来构建查询

queryParams = QueryParams.builder().withGroupings(new HashMap<String,List<String>>()).build();