Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a Spring Boot application via SpringBootTest

I'm new to Spring Boot and I really like it especially when it comes to eliminate the boilerplate code. I have created a test class to test my NBRController:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = NewBusinessRevitalizationApplication.class, 
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"management.port=0"})
public class NBRControllerTest extends TestCase {

    @LocalServerPort
    private int port;

    @Value("${local.management.port}")
    private int mgt;

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Test
    public void getApplicationByAgencyIdAndStatusTest() {
        String uri = "http://localhost:" + this.port + "/nbr-services/applications/{status}?agencyIds=123456,56765,678576";
        Map<String, String> vars = new HashMap<String, String>();
        vars.put("status", "SAVED");
        ResponseEntity<String> response = testRestTemplate.getForEntity(uri, String.class, vars);
        assertEquals(HttpStatus.OK, response.getStatusCode());
    }
}

If I run it in debug mode I can only debug the Test class and not my NBRController class:

@RestController
@RequestMapping("/nbr-services")
public class NBRController {

    @Autowired
    private NBRServices nbrServices;

    private static Logger logger = LoggerFactory.getLogger(NBRController.class);

    @RequestMapping(value = "/configuration/environment/{environment}", method = RequestMethod.GET)
    @ResponseBody
    public String getConfiguration(@PathVariable("environment") String environment) throws RemoteException {
        logger.debug("environment={}", environment);
        String result = nbrServices.getConfiguration(environment);
        return result;
    }
}

I have tried to setup the Tomcat debug port but not luck. The only way I can debug my NBRController is run it in debug mode and call my RestAPI from the browser, but I want to use my unit test. Thanks in advance!

like image 387
carlitos081 Avatar asked Feb 03 '17 15:02

carlitos081


1 Answers

I had this happening when I accidentally had 2 controller methods with the same path mapping.

Other alternatives for debugging:

Only mock the server using mockMVC

It is possible to debug a system by not using a split webEnvironment, but to use spring MockMVC to make direct method calls to controllers instead of http calls.

@SpringBootTest(
   webEnvironment = SpringBootTest.WebEnvironment.MOCK // this is the default
)
@AutoConfigureMockMvc
class MyTest {

  @Autowired
  private MockMvc mockMvc;

  @Test public void myTest() {
     mockMvc.perform("/mypath");
     // ...
  }
}

This will not actually make http calls between the jUnit class and the controller, so this http processing part would not be tested.

Start the server separately and attach a remote debugger

  1. In the IDE, start the application can in debugging mode
  2. When application it is up and running, start JUnit tests which contain any http client, e.g. RestAssured.

This will spawn 2 JVMs, but the IDE is connected to both, so all breakpoints work.

like image 106
tkruse Avatar answered Sep 19 '22 15:09

tkruse