In Linux, new line is treated as \n, whereas in Windows new line is treated as \r\n. Since new line character is different in different operating systems, we need a better way to handle this.
If you have new lines in the text, then use ‘getLogWithNormalizedLineSeparator’ method, it replace new line characters with \n.
Example
assertEquals(msg, systemErrRule.getLogWithNormalizedLineSeparator()); assertEquals(msg, systemOutRule.getLogWithNormalizedLineSeparator());
Find the below working application.
SystemOutErrNewLineCharTest.java
package com.sample.app.tests;
import static org.junit.Assert.assertEquals;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
public class SystemOutErrNewLineCharTest {
@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@Test
public void newLineText() {
String msg = "Hello\nHow\nAre\tYou\n";
System.err.print(msg);
System.out.print(msg);
assertEquals(msg, systemErrRule.getLogWithNormalizedLineSeparator());
assertEquals(msg, systemOutRule.getLogWithNormalizedLineSeparator());
}
}
You can download complete working application from this link.
https://github.com/harikrishna553/system-rules
No comments:
Post a Comment