Skip to content

Instantly share code, notes, and snippets.

@vmassol
Last active August 29, 2015 14:14
Show Gist options
  • Save vmassol/f3b7496b5bd3a9693fc2 to your computer and use it in GitHub Desktop.
Save vmassol/f3b7496b5bd3a9693fc2 to your computer and use it in GitHub Desktop.
Fail test if there's content output to stdout or stderr
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.runner.Description;
import org.junit.runner.notification.RunListener;
public class CaptureConsoleRunListener extends RunListener
{
private PrintStream savedOut;
private PrintStream savedErr;
private ByteArrayOutputStream collectingContentStream;
@Override
public void testStarted(Description description) throws Exception
{
this.collectingContentStream = new ByteArrayOutputStream();
PrintStream collectingPrintStream = new PrintStream(this.collectingContentStream);
// Capture stdout
this.savedOut = System.out;
System.setOut(collectingPrintStream);
// Capture stderr
this.savedErr = System.err;
System.setErr(collectingPrintStream);
}
@Override
public void testFinished(Description description) throws Exception
{
// Put back stdout
System.setOut(this.savedOut);
// Put back stderr
System.setOut(this.savedErr);
// If there is stdout or stderr content then fail the test
String outputContent = this.collectingContentStream.toString();
if (!outputContent.isEmpty()) {
throw new AssertionError("There should be no content output to the console by the test! Instead we got ["
+ outputContent + "]");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment