如何阻止JBehave修剪多行参数?

时间:2017-11-13 17:13:27

标签: bdd jbehave

我有一个步骤,我需要检查某些消息。我使用了一个多行参数:

Then I see welcome message: 'Welcome to our site! 
Some more text 
Even more text'

问题是消息应该在第一行和第二行的末尾有空格(IDK为什么,但它应该)。并且JBehave修剪每条线的空间。所以测试失败了。

如何设置不修剪此参数?

我尝试过编写\n而不是实际的换行符,但它没有用换行符代替

还尝试在参数的开头添加{trim = false},但它将其作为参数的一部分读取。

用引号做了一些实验(用过",',无),它没有帮助......

1 个答案:

答案 0 :(得分:2)

我已经测试过了,看来JBehave实际上在测试结束时截断了空格:

When some step with multitline parameter:first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces   
    @When("some step with multitline parameter:$lines")
    public void stepWithMultilneString(String s) {

        String lines [] = s.split("\\r?\\n");

        for(int i = 0; i < lines.length; i++) {
            System.out.format("Line %d is: >>%s<<\n", i+1, lines[i]);
        }
    }

Line 1 is: >>first line has 10 spaces at the end          <<
Line 2 is: >>Second line has 6 spaces at the end      <<
Line 3 is: >>Third line has no spaces at the end, but fourth line has 8 spaces<<
Line 4 is: >>        <<
Line 5 is: >>Fifth line has 3 spaces, and Sixth line has only 7 spaces<<
When some step with multitline parameter:first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces

我建议绕过 - 在文本的开头和结尾带有分隔符!的步骤:

When step with multitline parameter surrounded by !:!first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces   

!
    @When("step with multitline parameter surrounded by !:$string")
    public void stepWithMultilneStringVersion2(String s) {

        if( s.startsWith("!")) {
            s = s.substring(1);
        }
        if( s.endsWith("!")) {
            s = s.substring(0, s.length()-1);
        }

        String lines [] = s.split("\\r?\\n");

        for(int i = 0; i < lines.length; i++) {
            System.out.format("Line %d is: >>%s<<\n", i+1, lines[i]);
        }
    }

Line 1 is: >>first line has 10 spaces at the end          <<
Line 2 is: >>Second line has 6 spaces at the end      <<
Line 3 is: >>Third line has no spaces at the end, but fourth line has 8 spaces<<
Line 4 is: >>        <<
Line 5 is: >>Fifth line has 3 spaces, and Sixth line has only 7 spaces   <<
Line 6 is: >>       <<
When step with multitline parameter surrounded by !:!first line has 10 spaces at the end          
Second line has 6 spaces at the end      
Third line has no spaces at the end, but fourth line has 8 spaces

Fifth line has 3 spaces, and Sixth line has only 7 spaces   

!