tags:

views:

691

answers:

6

Hi! I'm running some JUnit tests on my applications. Every test has a for loop calling respective method 10000 times. The tested methods produce a lot of log. These logs are also automatically collected by JUnit as test output. This situation takes to OutOfMemoryError because the string buffer where JUnit keeps the output becomes too large. I dont' need these logs during tests, so if there is a way to tell JUnit "don't keep program output" it would be enough. Any ideas?

+1  A: 

Some options:

  1. Change your logging so that it dumps to a file instead of standard output.
  2. Increase the maximum heap size with -Xmx <some number>M, like -Xmx 256M.
matt b
+2  A: 

What type of logging are you using? Is there some way you can override the default logging behavior to just disregard all log messages?

Outlaw Programmer
A: 

I would just increase the available memory.. Try adding -Xmx256m -Xmx256m to your VM.

Jeremy
A: 

The solution was overriding logging properties. Now I disabled logging and all seems to work (test is still running). If it works I'll configure a way for logging to a file. Thanks everybody (and congratulations to Jeff & friends for this site).

loris_p
A: 

I see that an answer has been accepted already, but here's what I would have submitted if I had gotten it typed up and tested faster:

If by "logging" you mean System.out.println() or System.err.println(), and if you're sure that your test really doesn't need the logs, then you can redirect stdout and stderr programmatically.

// Save the original stdout and stderr
PrintStream psOut = System.out;
PrintStream psErr = System.err;
PrintStream psDevNull = null;
try
{
    // Send stdout and stderr to /dev/null
    psDevNull = new PrintStream(new ByteArrayOutputStream());
    System.setOut(psDevNull);
    System.setErr(psDevNull);
    // run tests in loop
    for (...)
    {
    }
}
finally
{
    // Restore stdout and stderr
    System.setOut(psOut);
    System.setErr(psErr);
    if (psDevNull != null)
    {
        psDevNull.close();
        psDevNull = null;
    }
}

This way, your test output will be disabled, but the other output from JUnit will not be, as it would be if you used redirection on the command line like this:

ant test &> /dev/null

The command line redirection causes all of Ant/JUnit's output to be redirected, not just what is coming from the class you are testing, so that's probably not what you want. The programmatic redirection causes only the prints/writes to System.out and System.err in your program to be redirected, and you will still get the output from Ant and JUnit.

shoover
A: 

If it's any help, setting outputtoformatters="no" solved all my memory problems (in Ant 1.7.1 it this prevents output generated by tests being sent to the test formatters).

Ewen Cartwright