Spring boot 如何通过SpringBootTest调试Spring启动应用程序

Spring boot 如何通过SpringBootTest调试Spring启动应用程序,spring-boot,testing,spring-test,Spring Boot,Testing,Spring Test,我是Spring Boot的新手,我真的很喜欢它,尤其是在消除样板代码方面。 我已经创建了一个测试类来测试我的控制器: @RunWith(SpringRunner.class) @SpringBootTest(类=NewBusinessResolvationApplication.class, webEnvironment=SpringBootTest.webEnvironment.RANDOM\u端口) @TestPropertySource(属性={“management.port=0”})

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

@RunWith(SpringRunner.class)
@SpringBootTest(类=NewBusinessResolvationApplication.class,
webEnvironment=SpringBootTest.webEnvironment.RANDOM\u端口)
@TestPropertySource(属性={“management.port=0”})
公共类NbControllerTest扩展了TestCase{
@本地服务器端口
专用int端口;
@值(${local.management.port}”)
私人内部管理;
@自动连线
私有TestRestTemplate TestRestTemplate;
@试验
public void GetApplicationByAgencyId和StatusTest()的{
字符串uri=”http://localhost:“+this.port+”/nbr services/applications/{status}?agencyIds=1234567678576”;
Map vars=newhashmap();
变量put(“状态”、“已保存”);
ResponseEntity response=testRestTemplate.getForEntity(uri,String.class,vars);
assertEquals(HttpStatus.OK,response.getStatusCode());
}
}
如果我在调试模式下运行它,我只能调试测试类,而不能调试我的
NBRController
类:

@RestController
@请求映射(“/nbr服务”)
公共类控制器{
@自动连线
私人服务;
私有静态记录器Logger=LoggerFactory.getLogger(NBRController.class);
@RequestMapping(value=“/configuration/environment/{environment}”,method=RequestMethod.GET)
@应答器
公共字符串getConfiguration(@PathVariable(“环境”)字符串环境)引发RemoteException{
debug(“environment={}”,environment);
字符串结果=nbrServices.getConfiguration(环境);
返回结果;
}
}
我试图设置Tomcat调试端口,但运气不好。
调试我的
NBRController
的唯一方法是在调试模式下运行它,并从浏览器调用RestAPI,但我想使用我的单元测试。提前谢谢

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

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

在Uri中添加主机+端口(这是不必要的),testRestTemplate会为您执行此操作。删除该选项,然后再试一次,您可能会遇到调试点

当我意外地使用了两个具有相同路径映射的控制器方法时,发生了这种情况

调试的其他备选方案:

仅使用mockMVC模拟服务器 可以通过不使用split 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处理部分

单独启动服务器并连接远程调试器
  • 在IDE中,可以在调试模式下启动应用程序
  • 当应用程序启动并运行时,启动包含任何http客户端的JUnit测试,例如重新启动

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

    这里是为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";
            }
            }
    

    就我个人而言,我使用的是
    maven-spring-boot
    插件,当我调试时,它来自一个maven运行配置。也许这就是你所做的错误所在?在测试阶段,
    maven-spring-boot
    插件将在测试运行之前启动spring-boot服务器


    如果您想使用命令行应用程序来执行,那么您必须在测试运行之前手动加载Spring上下文并从几行代码中执行主类。我不记得怎么做了。

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

  • 停止intellij中所有正在运行的实例。 2.只需将调试指针放在正确的控制器方法中,并在调试模式下运行测试用例
  • 除非你碰到了错误的端点,否则它应该是有效的
  • 此外,您还可以尝试在调试模式下的测试用例中评估testRestTemplate调用,以防它在网络本身出现故障

  • 你能解释一下如何调试RestController吗?我已更改为SpringBootTest.WebEnvironment.DEFINED_端口,您建议了该端口,但仍然跳过了制动点。在您的设置中,调试应该可以正常工作。需要检查的一件事是,您的rest调用将发送到您认为是的服务器。是否有其他未处于调试模式的实例正在运行?强制端口与规范不同,例如application.properties/yaml中的server.port=9123,然后在启动测试时执行netstat检查它是否已启动并正在运行。还可以尝试在postConstruct方法中的一行上放置一个断点,只是为了向自己证明,您正在使用测试中启动的应用程序。我使用的是正确的服务,因为@SpringBootTest启动了要调用的服务,问题是Intellij如果我在调试模式下运行单元测试,它会在调试模式下运行测试,而不是在服务中运行测试。您找到了这个问题的答案吗?据我所知,您需要的是嵌入式Spring应用程序上的远程调试功能。我也有类似的要求。看起来SpringBootTest正在内部运行。如果我有办法指定调试端口,我就能够做到这一点。到目前为止,我必须单独运行我的应用程序。如果这不起作用,请告诉我,我是通过手机应答的。明天将从pc发送错误消息:,从实际uri获取响应有点多余。Spring Boot test具有用于测试rest api的
    MockMvc
    。遵循本教程了解更多详细信息:这是您的实际代码吗?控制器中不存在您在测试中尝试连接到的终结点。