简体   繁体   English

如何使用 spock 框架在没有参数的情况下测试方法

[英]How to test method without args using the spock framework

I can't figure out how to test this.我不知道如何测试这个。 Tell me please good literature or vebinar on this topic请告诉我有关此主题的优秀文献或 vebinar

public InputMessenger getInputForMessengerFromConsole() {
    String templateValue;
    Map<String, String> valuesForTemplate = new HashMap<>();
    try (Scanner scanner = new Scanner(System.in, Constant.CHARSET_NAME_UTF_8)) {
        System.out.println(Constant.MESSAGE_INPUT_TEMPLATE);
        templateValue = scanner.nextLine();

        System.out.println(Constant.MESSAGE_NUMBER_OF_VALUES);
        int numberOfValues = scanner.nextInt();
        scanner.nextLine();

        IntStream.range(0, numberOfValues).forEach(index -> {
            System.out.println(Constant.MESSAGE_KEY + (index + Constant.INT_ONE) + Constant.COLON);
            String key = scanner.nextLine();
            System.out.println(Constant.MESSAGE_VALUE + (index + Constant.INT_ONE) + Constant.COLON);
            String value = scanner.nextLine();
            valuesForTemplate.put(key, value);
        });
    }
    return new InputMessenger(templateValue, valuesForTemplate);
}

This is a draft (I didn't check it):这是草稿(我没看):

setup: 
def originalIn = System.in
def originalOut = System.out

and: "Define new out:"

def out = new ByteArrayOutputStream()
System.setOut(new PrintStream(out))

and: "Define input data and specify in"

def input = "…input data for test…"
def in = new ByteArrayInputStream(input.getBytes())
System.setIn(in)

when:
def result = getInputForMessengerFromConsole()

then:
out.toString() == expectedOutput

and:
result == expectedResult

cleanup:
System.setOut(originalOut)
System.setIn(originalIn)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM