0

I'm using JUnit 5 and the spring-boot-starter-test (v 1.5) for Maven. I would want to test a controller method using mock MVC ...

@ExtendWith(MockitoExtension.class)
public class MyControllerTest {
    ...
      MockMvc mockMvc;
    ...

  @BeforeEach
  public void setup() throws Exception {
    ...
    mockMvc =
        MockMvcBuilders.standaloneSetup(controller)
            .setControllerAdvice(new RestResponseEntityExceptionHandler())
            .build();

  }


  void testGetMethod() throws Exception {
    ... mocking ...

    mockMvc
        .perform(get("/mypath").contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk());
  }

Is there a way to get a more verbose stack trace when the test fails? When I run the test and there is a NullPointerException within the controller, the stack trace only prints the line where the unit test was failing

> mvn clean test -Dtest=MyControllerTest
...
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.676 s <<< FAILURE! - in com.rmyco.feature.controller.MyControllerTest
[ERROR] com.rmyco.feature.controller.MyControllerTest.testGetMethod  Time elapsed: 1.663 s  <<< ERROR!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
        at com.rmyco.feature.controller.MyControllerTest.testGetMethod(MyControllerTest.java:103)
Caused by: java.lang.NullPointerException
        at com.rmyco.feature.controller.MyControllerTest.testGetMethod(MyControllerTest.java:103)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   MyControllerTest.testGetMethod:103 » NestedServlet

I would like to know the exact line in the controller where things were blowing up.

Dave
  • 15,639
  • 133
  • 442
  • 830
  • have you tried [this solution](https://stackoverflow.com/questions/2928548/make-mavens-surefire-show-stacktrace-in-console)? – Andrey B. Panfilov Jul 25 '22 at 17:14
  • Looks like you got a null pointer at line 103 of your test class. If you add the code we can see what cloud be wrong – Willem Jul 25 '22 at 19:05

0 Answers0