Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit参数化测试显示换行符_Java_Junit_Parameterized - Fatal编程技术网

Java JUnit参数化测试显示换行符

Java JUnit参数化测试显示换行符,java,junit,parameterized,Java,Junit,Parameterized,如果您试图显示换行符,JUnit将自动失败,这是一个已知的错误: 导入静态org.junit.Assert.assertEquals; 导入java.util.array; 导入java.util.Collection; 导入org.junit.Test; 导入org.junit.runner.RunWith; 导入org.junit.runners.Parameterized; 导入org.junit.runners.Parameterized.Parameters; @RunWith(参数化

如果您试图显示换行符,JUnit将自动失败,这是一个已知的错误:

导入静态org.junit.Assert.assertEquals;
导入java.util.array;
导入java.util.Collection;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.junit.runners.Parameterized;
导入org.junit.runners.Parameterized.Parameters;
@RunWith(参数化的.class)
公开课范例{
私有字符串实际值;
应为私有字符串;
公共示例(实际字符串,预期字符串){
this.actual=实际;
this.expected=expected;
}
@参数(name=“{0}”)//无法执行此操作(“\n”不允许)
公共静态集合testCollection(){
返回Arrays.asList(新对象[][]{
{“你好\n世界”,“你好\n世界”}
});
}
@试验
公开无效测试(){
资产质量(预期、实际);
}
}

是否有任何已知的解决此问题的方法?例如,是否有方法替换此处的换行符:
@Parameters(name=“{0}”)
,但实际上不是在测试本身中?

您需要双转义斜杠,如
\\n

尝试双转义斜杠?就像
\\n
我浪费了一个小时试图找出问题所在,而你在大约一分钟内解决了我的问题。。。谢谢:)它叫“我以前遇到过同样的问题”(又名经验)——很高兴我能帮上忙
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class Example {

    private String actual;
    private String expected;

    public Example(String actual, String expected) {
        this.actual = actual;
        this.expected = expected;
    }

    @Parameters(name = "{0}") // can't do this ("\n" not allowed)
    public static Collection<Object[]> testCollection() {
        return Arrays.asList(new Object[][] {
            { "Hello\nWorld", "Hello\nWorld" }
        });
    }

    @Test
    public void test() {
        assertEquals(expected, actual);
    }

}