内容相同但 JUnit 测试失败(IntelliJ)
Identical Content But JUnit Test Fails(IntelliJ)
我有一个比较两个字符串的 JUnit 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
@Rule
public final SystemOutRule log = new SystemOutRule().enableLog();
@Autowired
private CompactDisc cd;
@Autowired
private MediaPlayer player;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
@Test
public void play() {
player.play();
assertEquals("Playing title Sgt. Pepper's Lonely Hearts Club Band by artist The Beatles\n",
log.getLog());
}
}
我在第二次测试中遇到 org.junit.ComparisonFailure
异常。但是,IntelliJ 显示的内容是相同的。我错过了什么?
编辑:在预期字符串的末尾添加回 \n。
您的结束行符号不同(LF 与 CRLF)
尝试添加 log.getLog().trim()
并从预期字符串的末尾删除 \n。
我有一个比较两个字符串的 JUnit 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest {
@Rule
public final SystemOutRule log = new SystemOutRule().enableLog();
@Autowired
private CompactDisc cd;
@Autowired
private MediaPlayer player;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
@Test
public void play() {
player.play();
assertEquals("Playing title Sgt. Pepper's Lonely Hearts Club Band by artist The Beatles\n",
log.getLog());
}
}
我在第二次测试中遇到 org.junit.ComparisonFailure
异常。但是,IntelliJ 显示的内容是相同的。我错过了什么?
编辑:在预期字符串的末尾添加回 \n。
您的结束行符号不同(LF 与 CRLF)
尝试添加 log.getLog().trim()
并从预期字符串的末尾删除 \n。