问题
I have a step where I need to check certain message. I used a multi-line parameter:
Then I see welcome message: 'Welcome to our site!
Some more text
Even more text'
Problem is that message should have spaces at the end of first and second lines (IDK why, but it should). And JBehave trims spaces from each line. So test fails.
How can I set it not to trim this parameter?
I've tried writing \n
instead of actual line-breaks, but it wasn't replaced with line-breaks
Also tried adding {trim=false} in the beginning of parameter, but it reads it as part of the parameter.
Made some experiments with quotation marks (used ", ', none), it didn't help either...
回答1:
I have tested that and it seems that JBehave actually truncates spaces at the end of the test:
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
I suggest a bypass - a step with delimiters !
at the beginning and end of the text:
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
!
来源:https://stackoverflow.com/questions/47269885/how-to-stop-jbehave-from-trimming-multiline-parameters