This Question is Answered

2 "helpful" answers available (2 pts)
1 Replies Last post: Jul 7, 2013 10:34 PM by Mark Vedder  
mars009 Newbie 1 posts since
Jul 7, 2013
Currently Being Moderated

Jul 7, 2013 10:04 AM

JUnit tests not printing stack trace when an exception is thrown.

Hi guys,

I believe this is a very nooby question, and I apologize for it in advance. I was using STS to run the Sample Spring Batch project and the JUnit test class that comes with it. Whenever there is an exception within any of the classes that compose the whole test, I get a prinout to the console with the Exception and the stack trace (The tests are marked as passed).

 

This doesn't seem to be the case in IntelliJ, and while I do get the print outs from every "sout" declared within the classes, any thrown exceptions are never "sout" into the console. Am I doing something wrong or am I missing anything within the configurations from IntelliJ? How can I get it to print out any exception like STS does? I believe I have this working one day, but I just can't remember what I did .

 

Thanks in advance for any help!

Mark Vedder Apprentice 971 posts since
Dec 10, 2003
Currently Being Moderated
Jul 7, 2013 10:34 PM in response to: mars009
Re: JUnit tests not printing stack trace when an exception is thrown.

Most likely the stacktrace is being output to standard error rather than standard out. The exception.printStackTrace() method outputs to Standard error, and is the most common way example code outputs a stacktrace.

When you run unit tests in IDEA, it shows standard out for all tests. But when viewing an individual test result, it only shows standard error if that test failed. You can however see the full output (standard out and standard error) by looking at the cumulative results for the class. Here's an example...

 

The following test class has two tests that each print a line to standard out, and a line to standard error. One test will pasee, the other will fail.

public class SimpleTest
{
    @Test
    public void test1() throws Exception
    {
        System.out.println("1: Standard Out Message");
        System.err.println("1: Standard Error Message");
        Assert.assertTrue(true);
 
    }
 
    @Test
    public void test2() throws Exception
    {
        System.out.println("2: Standard Out Message");
        System.err.println("2: Standard Error Message");
        Assert.assertTrue(false);
    }
}
 

 

Looking at the results for test 1 (that passed) we do not see the standard error output, just the standard out.

Test_1_results.png

The results for test 2 (that failed) show the standard error as well as the standard out

Test_2_results.png

Finally, the results for the class show all the output to the console...

Test_class_results.png

So you'll just need to look at the full console output to view the resuls.

More Like This

  • Retrieving data ...