Environment: junit 4.12, system-rules 1.19.0, Intellij IDEA, Maven
Problem: Spring in Action 4th, the test java file, soundsystem#CDPlayerTest, to test the autowired, using StandardOutputStreamLog to get the system standard output, but I get the debug information. So I read the docs and used the SystemOutRule, still get the same result. If I use this tool to redirect system standard output, I can get the expected result.
Here is the code.
1.Using SystemOutRule
code:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {CompactDiscConfig.class})
public class ComponentScanTest {
@Autowired
private MediaPlayer player;
@Rule
public SystemOutRule log = new SystemOutRule().enableLog();
@Test
public void autoWiredTest() {
System.out.println("Play Beautiful World(DC.Ver) by Hikaru Utada");
Assert.assertEquals(log.getLog(), "Play Beautiful World(DC.Ver) by Hikaru Utada\r\n");
}
}
the content of log.getLog():
21:18:22.574 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class soundsys.ComponentScanTest]
21:18:22.579 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
...
2.Using redirect
code:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {CompactDiscConfig.class})
public class ComponentScanTest {
@Autowired
private MediaPlayer player;
@Rule
public SystemOutRule log = new SystemOutRule().enableLog();
@Test
public void autoWiredTest() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.out.println("Play Beautiful World(DC.Ver) by Hikaru Utada");
Assert.assertEquals(out.toString(), "Play Beautiful World(DC.Ver) by Hikaru Utada\r\n");
}
}
the content of out.toString():
Play Beautiful World(DC.Ver) by Hikaru Utada
But what puzzles me most is , the followed example of SystemOutRule can get the sout, but if using junit in spring I can't get the same result
example which is test passed:
public class MyTest {
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@Test
public void writesTextToSystemOut() {
System.out.print("hello world");
assertEquals("hello world", systemOutRule.getLog());
}
}
my code which is test failed, because result of getLog() is the debug infos:
@RunWith(SpringJUnit4ClassRunner.class)
public class MyTest {
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@Test
public void writesTextToSystemOut() {
System.out.print("hello world");
assertEquals("hello world", systemOutRule.getLog());
}
}
Environment: junit 4.12, system-rules 1.19.0, Intellij IDEA, Maven Problem: Spring in Action 4th, the test java file,
soundsystem#CDPlayerTest
, to test the autowired, using StandardOutputStreamLog to get the system standard output, but I get the debug information. So I read the docs and used the SystemOutRule, still get the same result. If I use this tool to redirect system standard output, I can get the expected result.Here is the code. 1.Using SystemOutRule code:
the content of log.getLog():
2.Using redirect code:
the content of out.toString():
But what puzzles me most is , the followed example of SystemOutRule can get the sout, but if using junit in spring I can't get the same result example which is test passed:
my code which is test failed, because result of getLog() is the debug infos: