4

When an Exception occurs while running a JUnit test (an unexpected Exception outside the scope of the test), no stacktrace is printed. Only the Exception message is. For example, I'm adding unit tests using JUnit to an existing Spring MVC project. I've got an empty test like this:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/my-servlet.xml")
public class UserAccessTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Test
    public void test01() {}
}

But it fails with the message:

Tests in error: 
  test01(com.myproject.UserAccessTest): Failed to load ApplicationContext

However, it doesn't print the entire stack trace, including root causes, etc. Another example of this happening is a NullPointerException occurring when I've forgotten to mock certain objects when I'm using Mockito. In such a case, it doesn't even give a message, and it's not until after I add a try-catch block in the test that I find out it's a NullPointerException, and then it isn't until I add code to manually print all StackTraceElements in the catch that I find out where it's at!

Is it possible to have the entire stacktrace printed when exceptions occur during the test?

I'm using Maven and IntelliJ IDEA. I'm not sure though whether this is even something I have to configure in either one of these. I've found this somewhat related answer but 1) that involves Ant on a build server and 2) I have no clue where I would have to put that config if it would apply to me since that's not mentioned in the answer.

Please note: I'm not asking for help solving the issue in the example. I just want to know how I can get JUnit to provide more information about such problems so that I can debug it further myself.

Community
  • 1
  • 1

1 Answers1

1

There are some surefile plugin settings (eg maven-surefire-plugin useFile parameter set to false) that control this, refer to this

Community
  • 1
  • 1
stringy05
  • 6,511
  • 32
  • 38
  • 1
    I ended up setting `useFile` and `trimStackTrace` both to false. Thank you very much! I was able to make more progress in the past 10 minutes than in hours prior to this. – Ricardo van den Broek Sep 16 '14 at 20:58