繁体   English   中英

JUnit参数化测试显示换行符

[英]JUnit Parameterized Tests Display Newline

这是一个已知的错误,如果您尝试显示换行符,JUnit的参数化测试将无提示地失败: https : //bugs.eclipse.org/bugs/show_bug.cgi? id =474465

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);
    }

}

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

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

暂无
暂无

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

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