Java Concordion substring(“contains”)的解决方法

时间:2017-05-29 10:10:12

标签: java testing concordion

正如我所见,Concordion测试框架只能使精确的字符串/数字匹配。

是否有任何解决方法来测试结果是否包含一些子字符串?

例如,我测试服务返回格式为ERROR 123的错误,测试应对任何错误编号有效。

2 个答案:

答案 0 :(得分:1)

根据所需的输出可读性和您想要投入的精力,有许多答案。

1)最简单的方法是使用assert-true检查子字符串,例如:

The service [returns](- "#result=callService()") an [ERROR](- "c:assert-true=containsSubstring(#result, #TEXT)")

with fixture:

@RunWith(ConcordionRunner.class)
public class StackOverflow {

    public String callService() {
        // call your service here
    }

    public boolean containsSubstring(String input, String check) {
        return input.contains(check);
    }
}

这样做的缺点是,失败时,错误信息只会显示:

message showing '== false'

2)与1相同的夹具,containsSubstring在失败时抛出异常。然后,Concordion会将故障显示为堆栈跟踪:

public boolean containsSubstring(String input, String check) {
    if (!input.contains(check)) {
        throw new AssertionError("'" + input + "' does not contain '" + check + "'");
    }
    return true;
}

失败时显示:

stack trace showing '200 OK' does not contain 'ERROR'

3)使用assertEquals检查:

The service [returns](- "#result=callService()") an [ERROR](- "?=contains(#result, #TEXT)")

with fixture:

public String contains(String input, String check) {
    if (!input.contains(check)) {
        return input;
    }
    return check;
}

这显示了最好的"错误消息:message showing 'ERROR' in strikethrough, then '200 OK'

4)你可以写一个扩展,这可以避免你在每个夹具中定义这样的contains方法(或者从一个超类夹具中继承它们):

规格:

The service [returns](- "#result=callService()") an [ERROR](- "cx:isError=#result")

夹具:

@RunWith(ConcordionRunner.class)
@ConcordionOptions(declareNamespaces={"cx", "urn:error-extension:2017"})
@Extensions(ErrorExtension.class)
public class StackOverflow {

    public String callService() {
        // call your service here
    }
}

扩展:

public class ErrorExtension implements ConcordionExtension {
    private List<AssertListener> listeners = new ArrayList<AssertListener>();

    public void addAssertEqualsListener(AssertListener listener) {
        listeners.add(listener);
    }

    public void removeAssertEqualsListener(AssertListener listener) {
        listeners.remove(listener);
    }

    @Override
    public void addTo(ConcordionExtender concordionExtender) {
        concordionExtender.withCommand("urn:error-extension:2017", "isError", new AbstractCommand() {
            @Override
            public void verify(CommandCall commandCall, Evaluator evaluator, ResultRecorder resultRecorder) {
                Check.isFalse(commandCall.hasChildCommands(), "Nesting commands inside an 'isError' is not supported");

                Element element = commandCall.getElement();

                String actual = (String) evaluator.evaluate(commandCall.getExpression());

                if (actual.contains("ERROR")) {
                    resultRecorder.record(Result.SUCCESS);
                    announceSuccess(element);
                } else {
                    resultRecorder.record(Result.FAILURE);
                    announceFailure(element, "String containing ERROR", actual);
                }
            }
        });
        listeners.add(new AssertResultRenderer());
    }

    private void announceSuccess(Element element) {
        for (AssertListener listener : listeners) {
            listener.successReported(new AssertSuccessEvent(element));
        }
    }

    private void announceFailure(Element element, String expected, Object actual) {
        for (AssertListener listener : listeners) {
            listener.failureReported(new AssertFailureEvent(element, expected, actual));
        }
    }
}

也显示了最好的&#34;错误消息:message showing 'ERROR' in strikethrough, then '200 OK'

答案 1 :(得分:0)

为了测试你的逻辑,你可以去 http://www.vogella.com/tutorials/Mockito/article.html