I'm writing a JUnit test case which invokes code that ultimately results in a native
function call being invoked. Here's pseudocode of the test I want to write:
@Test
public void testNoWritesToStdOut() {
x = new StdOutTrap();
x.startTrappingStdOut();
try {
callTheFunctionUnderTest(); // Nothing should be written to stdout
assertEquals("", x.capturedOutput());
} finally {
x.stopTrappingStdOut();
}
}
Noting that callTheFuntionUnderTest()
invokes native
code which might be C code, as in:
...
printf("fail\n");
Or C++ code, as in:
...
std::cout << "fail" << std::endl;
For clarity, I don't want to redirect calls to System.out.X
to a custom PrintStream
via System.setOut
. I want to get a copy of the bytes, if any, which were written to the stdout
standard stream during the execution of a native
call over which I have no control.
Ideas?
If you want to capture output from native code, you cannot do this in the JVM alone but need the operating system to help you.
You must invoke your test in a separate JVM and you must ask the operating system to capture the output for you. The usual way of running a process in a shell might be what you need.