测试一个奇怪的服务功能

Testing a weird Service function

我正在测试 Spring 引导服务,有一个非常奇怪的功能需要测试。我做了一个成功运行的测试,但我没有得到太多的报道。这是我正在测试的功能:

protected String setQueryListField(List<String> model, String fieldName) {
    String query = "";
    if (model.contains("is blank") && model.size() > 1) {
        for (String value : model) {
            if (value.equals("is blank")) {
                query = query.concat(" AND (" + fieldName + " = null");
            } else if (model.size() == 2) {
                query = query.concat(" OR " + fieldName + " CONTAINS('" + value + "'))");
            } else {
                if (model.indexOf(value) == 1) {
                    query = query.concat(" OR " + fieldName + " CONTAINS('" + value + "', ");
                } else if ((model.indexOf(value) + 1) == model.size()) {
                    query = query.concat("'" + value + "'))");
                } else {
                    query = query.concat("'" + value + "', ");
                }
            }
        }
    } else if (model.contains("is blank") && model.size() == 1) {
        query = query.concat(" AND " + fieldName + " = null");
    } else {
        query = query.concat(" AND " + fieldName + " CONTAINS('" + String.join("','", model) + "')");
    }
    return query;
}

这是我写的测试。正如我所说,它运行成功,但我希望获得更多报道。你有什么建议吗?

@Test
@DisplayName("setQueryListField Test")
void setQueryListField() {
    List<String> stringList = new ArrayList<>();
    stringList.add("is blank");

VaultService vaultService = Mockito.spy(vaultServiceTest);
doReturn("", "").when(vaultService).setQueryListField(stringList, "field__name");
String response = vaultService.setQueryListField(stringList, "field__name");

System.out.println(response);

stringList.add("test");
String secondResponse = vaultService.setQueryListField(stringList, "external__c");
System.out.println(secondResponse);

}

您只涵盖以下 if 块

else if (model.contains("is blank") && model.size() == 1) {
        query = query.concat(" AND " + fieldName + " = null");
    }

如果您需要更多的覆盖范围,您需要尝试使用其他输入,以便它也覆盖其他分支。例如,model.size() > 1.

此外,以下几行不是操作,因为它模拟的是您要测试的相同功能。

VaultService vaultService = Mockito.spy(vaultServiceTest);
doReturn("", "").when(vaultService).setQueryListField(stringList, "field__name");
String response = vaultService.setQueryListField(stringList, "field__name");