0

I have the following Junit test

@Before
public void setup() {
UUTClass myObject = new UUTClass();
}

@After
public void teardown() {
// cleanup
}

@Test
public void testSomeMethod() {
myObject.invokeSomeMethod(); // This is throwing NPE from somewhere inside my UUTClass
}

I don't want to use expected=NullPointerException.class what I want is log the stack trace from my UUTClass. All I can see is a NullPointerException one liner in my test method.

I am using Log4j to log everything and I have got log4j.xml in the search path which does get picked up by Initializer. I can see my logger messages being printed for other items.

How to enable Junit to return/propagate the full stack trace for NPE that's been thrown by my UUTClass? I believe it's probably because the errors are redirected to error console and perhaps not being picked up by log4j (don't know if I explained that right).

As a workaround, I will probably use printStackTrace() for now. but ideally I would like to log it if possible.

My Intention is the first understand where in the design I have missed out conditions/constraints which resulted into this NPE and then build more tests to expect certain exceptions. Regards,

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • Are you using maven? – Vinay Prajapati Feb 28 '18 at 12:42
  • yes I am - Running maven test. My Logger is only logging NPE, but it seems calling `printStackTrace()` does what I want. I will need to update my question. – ha9u63a7 Feb 28 '18 at 13:05
  • you could close the question probably. – Vinay Prajapati Feb 28 '18 at 13:23
  • it's a user specific problem, not related to API. – ha9u63a7 Feb 28 '18 at 13:24
  • Usually logging frameworks have methods that accept a message and an exception to log the stacktrace for, such as `logger.error("Unable to open connection", myCaughtException)` ([related question](https://stackoverflow.com/questions/4347797/how-to-send-a-stacktrace-to-log4j)). How and where are you logging your NPE? It does not seem to be in the test, so I assume it's in your `UUTClass`, so how do you call the logging framework there? – Morfic Feb 28 '18 at 15:35

0 Answers0