2015-10-14 57 views
1

这是一个已知的bug是JUnit的Parameterized Tests如果你试图显示换行符将失败默默:https://bugs.eclipse.org/bugs/show_bug.cgi?id=474465JUnit的参数化测试显示换行符

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

尝试双逃避斜线? like'\\ n' – ochi

+0

我浪费了一个小时,试图弄清楚什么是错误的,并且你在一分钟内解决了我的问题......谢谢:) –

+0

它被称为“我以前遇到过同样的问题”(又名体验) - 很高兴我能帮上忙 – ochi

回答

1

您需要双击逃避斜线,如\\n