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!
I had this happening when I accidentally had 2 controller methods with the same path mapping.
Other alternatives for debugging:
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.
This will spawn 2 JVMs, but the IDE is connected to both, so all breakpoints work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With