向Spring Boot贡献代码时,如何通过命令行正确运行指定单元测试?
解决Spring Boot指定单元测试运行失败的问题
别发愁,这个问题根本不用你先去啃一堆Gradle知识就能解决!核心原因是你找错了测试所在的Gradle模块。
你要运行的org.springframework.boot.actuate.endpoint.EndpointIdTests属于spring-boot-actuator模块,而不是你当前命令默认扫描的spring-boot模块。直接在根目录执行./gradlew test时,Gradle会去spring-boot模块里找测试类,自然会报“找不到测试”的错误。
给你两个直接能用的命令:
- 用全类名指定测试(最稳妥,避免重名):
./gradlew :spring-boot-project:spring-boot-actuator:test --tests org.springframework.boot.actuate.endpoint.EndpointIdTests - 如果模块里没有同名测试类,也可以简化成只用类名:
./gradlew :spring-boot-project:spring-boot-actuator:test --tests EndpointIdTests
额外给你个小技巧:以后遇到类似问题,直接在项目里搜索测试类的文件位置——比如EndpointIdTests在spring-boot-project/spring-boot-actuator/src/test/java/...目录下,对应的Gradle模块路径就是:spring-boot-project:spring-boot-actuator,把这个路径加在gradlew后面,再跟上test --tests就能精准定位啦。
内容的提问来源于stack exchange,提问作者Jim Stockwell
相关产品推荐
相关产品推荐

