如何从SpringBootTest调试Spring Boot应用程序

时间:2017-02-03 15:09:08

标签: debugging spring-boot spring-boot-test

我是Spring Boot的新手,我非常喜欢它,特别是在消除样板代码方面。 我创建了一个测试类来测试我的@RestController:

@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());
}
}

如果我在调试模式下运行它,我只能调试Test类而不是我的@RestController类:

@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;
}

我试过设置Tomcat调试端口,但没有运气。 我可以调试@RestController的唯一方法是在调试模式下运行它并从浏览器调用我的RestAPI,但我想使用我的单元测试。提前致谢。

6 个答案:

答案 0 :(得分:1)

您可能在与您认为自己的端口不同的端口上运行。

SpringBootTest注释控制着测试端口,例如

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

答案 1 :(得分:1)

当我不小心有两个具有相同路径映射的控制器方法时,发生了这种情况。

其他调试方式:

仅使用模拟MVC模拟服务器

可以通过不使用拆分的webEnvironment来调试系统,而可以使用spring MockMVC对控制器进行直接方法调用,而不是使用HTTP调用。

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

  @Autowired
  private MockMvc mockMvc;

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

这实际上不会在jUnit类和控制器之间进行http调用,因此将不测试此http处理部分。

单独启动服务器并附加一个远程调试器

  1. 在IDE中,可以在调试模式下启动应用程序
  2. 在应用程序启动并运行时,启动包含任何http客户端的JUnit测试,例如RestAssured。

这将产生2个JVM,但是IDE都连接到这两个JVM,因此所有断点都可以工作。

答案 2 :(得分:0)

在Uri中你附加http://local主机+端口,这是不必要的,testRestTemplate会为你做。删除它,并尝试你可以点击调试点

答案 3 :(得分:0)

这里是为Spring boot 2 + JUnit 5编写Junit for Rest层的示例

@ExtendWith(MockitoExtension.class)
public class RestTest {

    @InjectMocks
    private Rest  rest;
    
    private MockMvc mockMvc;
    
    @BeforeEach
    public void init() throws Exception {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(rest).build();
    }
    
    @Test
    public void getTest() throws Exception {
        String url = "/test";
        ResultActions resultActions = mockMvc.perform(get(url));
        resultActions.andExpect(status().isOk());

    }
    
}

    @RestController
    @RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    public class Rest  {
    
        
        
        @GetMapping
        public @ResponseBody String get()  {
            
            return "success";
        }
        }

答案 4 :(得分:0)

我个人使用maven-spring-boot插件,当我调试时,它来自Maven运行配置。也许那是你在做什么错? maven-spring-boot插件在测试阶段将在测试运行之前启动spring boot服务器。

如果要使用命令行app进行操作,则必须在测试运行之前手动加载Spring上下文并通过几行代码执行主类。我不记得该怎么做。

答案 5 :(得分:0)

我使用的是 Intellij 2020.3,我可以调试我的控制器。

  1. 从intellij 停止所有正在运行的实例。 2. 只需将调试指针放在正确的控制器方法中,并在调试模式下运行您的测试用例。
  2. 除非您遇到错误的端点,否则它应该可以工作。
  3. 您也可以尝试在调试模式下的测试用例中评估您的 testRestTemplate 调用,以防网络本身失败。
相关问题