JUnit参数化测试显示换行符

时间:2015-10-14 16:46:34

标签: java junit parameterized

如果您尝试显示换行符,JUnit的Parameterized Tests将无声地失败,这是一个众所周知的错误: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}"),但实际上不在测试本身?

1 个答案:

答案 0 :(得分:1)

您需要双击斜杠,例如\\n