如何通过 SpringBootTest 调试 Spring Boot 应用程序

问题描述 投票:0回答:6

我是 Spring Boot 的新手,我真的很喜欢它,尤其是在消除样板代码方面。 我创建了一个测试类来测试我的

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

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

NBRController
类:

@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 调试端口,但不成功。 我调试

NBRController
的唯一方法是在调试模式下运行它并从浏览器调用我的 RestAPI,但我想使用我的单元测试。预先感谢!

spring-boot testing spring-test
6个回答
1
投票

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

其他调试替代方案:

仅使用mockMVC模拟服务器

可以不使用分割的 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 测试,例如放心。

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


1
投票

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

  1. 停止 intellij 中所有正在运行的实例。
  2. 只需将调试指针放入正确的控制器方法中,然后在调试模式下运行测试用例即可。
  3. 除非你到达了错误的端点,否则它应该可以工作。
  4. 您还可以尝试在调试模式下的测试用例中评估您的 testRestTemplate 调用,以防网络本身失败。

0
投票

您运行的端口可能与您认为的端口不同。

SpringBootTest 注解用于控制测试端口,例如

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

0
投票

在您要附加的 Uri 中 http://local host +port ,这不是必需的, testRestTemplate 会为您完成。删除它并尝试您可能会达到调试点


0
投票

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

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

0
投票

就个人而言,我使用

maven-spring-boot
插件,当我调试时,它来自 Maven 运行配置。也许这就是你所做的事情的问题所在?
maven-spring-boot
插件在测试阶段将在测试运行之前启动 Spring Boot 服务器。

如果您想使用命令行应用程序来执行此操作,则必须在测试运行之前手动加载 Spring 上下文并通过几行代码执行主类。我不记得如何做。

© www.soinside.com 2019 - 2024. All rights reserved.