4

I am not sure if it is possible but if so how do I do it? I have a project which contains another 2 projects as dependencies. The parent project is only used for testing purposes (client server app). When I am testing I have a difficulty reading through my testing output because of the large amount of output the client and the server projects have. I am trying to find a way hide from the console all the output(printf()) of my sub-projects so that I can only see my testing output. Is there a way to do so?

For testing I am using JUnit.

Thanks

Rakim
  • 1,087
  • 9
  • 21
  • 40

3 Answers3

2

You should use Java Logger API (or Log4J) instead of using System.out.print directly, sou you could disable specific loggers during executions of your tests.

If you can't change legacy code to use Logger API, there are two options:

Option 1:

Create a PrintStream decorator class like this pseudo-code:

public class FilteredPrintStream extends PrintStream {

    private final PrintStream stream;

    ...

    @Override
    public void print(String str) {
        if (notCalledBySubproject()) {
            stream.print(str);
        }
    }

    // override other methods too
}

Then you set default output: System.setOut(new FilteredPrintStream(System.out));

To find method caller (to create notCalledBySubproject method), see How do I find the caller of a method using stacktrace or reflection?

Option 2:

Use Logger API in your test code, so you can redirect your output to a file, for example, then you ignore console output and see the file generated by your tests.

Community
  • 1
  • 1
andrucz
  • 1,971
  • 2
  • 18
  • 30
  • It is impossible to go through all of the application classes and start changing all the system outputs to the Java Logger. It is more of the case that there is either a way to do it as is or leave it as is – Rakim Dec 07 '15 at 20:14
  • Ok. Using System.out.print is really a bad practice in a code that should be reusable. There is no way to do exactly what you want without loggers, but take a look at System.setOut method. – andrucz Dec 07 '15 at 20:17
  • Just edited my answer again, including one more option. – andrucz Dec 07 '15 at 21:09
0

It is not a perfect solution nor good coding practice but you could add a class like this

public class SystemOutput
{
  public static final boolean DO_PRINT = true;

  public void printf(String format, Object... args)
  {
     if(DO_PRINT)
       System.printf(format, args);
  }
}

and use Find and Replace once to replace "System.out.printf" with "SystemOutput.printf" in every class needed. Because both methods have the same declaraction only this has to be changed. When you want to block the output you can just set DO_PRINT to false.

Eclipse for example provides a search tool which can find and replace a certain string in every .java file in a project. (Strg + H under the File Search tab)

of course it is also possible to call System.setOut() with your own subclass of PrintStream, that overrides the printf() method to only print when a certain boolean value is true.

Tobias
  • 178
  • 1
  • 9
  • thank you for the suggestion but using switches for the system output is way too much. especially when they only need to be hidden for testing purposes. If I was about to go through so much refactoring code, I could just go for Logger as @andrucz suggested – Rakim Dec 07 '15 at 20:32
  • added another possibility which is kind of a mixture between both solutions – Tobias Dec 07 '15 at 20:34
0

why do you need console to run unit tests? ignore it. if your tests passes you got 0 status code or green bar (IDE or jenkins). any error stack trace you can find in e.g. maven log tests results. just ignore the std output

another thing: using console in your application is usually bad idea - avoid it. use logging framework instead (it will let you control the destination and level of logging). use your IDE and refactor - replace all calls to printf with log.debug or with you own wrapper. if your IDE doesnt support it then use some regex and try replace-all

if you want to get rid of all the output you can redirect stdout to /dev/null or change output stream in java. but it's not a proper solution

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • Thanks for the advice. I am currently just ignoring everything as you said. Was just wondering is it is possible to get rid of them. But will probably refactor at some later point all my `printf`s – Rakim Dec 08 '15 at 14:44