Spring Boot无法找到测试类:REST控制器测试执行失败
问题:Spring Boot测试类无法被Gradle识别执行
我在为一个返回String的REST控制器编写测试,单独运行测试类时,Gradle提示找不到对应测试用例。
控制器代码
package com.sample.testing; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/") public String index() { return "Greetings from Spring Boot!"; } }
测试类代码
import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void shouldReturnDefaultMessage() throws Exception { mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()); } }
运行测试时的错误信息
Execution failed for task ':test'. > No tests found for given includes: [com.sample.testing.HelloControllerTest](--tests filter)
Gradle构建配置
plugins { java id("org.springframework.boot") version "3.2.9" id("io.spring.dependency-management") version "1.1.6" id("org.graalvm.buildtools.native") version "0.9.28" id("org.asciidoctor.jvm.convert") version "3.3.2" } group = "com.sample" version = "0.0.1-SNAPSHOT" java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } configurations { compileOnly { extendsFrom(configurations.annotationProcessor.get()) } } repositories { mavenCentral() } extra["snippetsDir"] = file("build/generated-snippets") dependencies { implementation("org.springframework.boot:spring-boot-starter-data-rest") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-web-services") compileOnly("org.projectlombok:lombok") developmentOnly("org.springframework.boot:spring-boot-devtools") annotationProcessor("org.projectlombok:lombok") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.boot:spring-boot-testcontainers") testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc") testImplementation("org.testcontainers:junit-jupiter") testRuntimeOnly("org.junit.platform:junit-platform-launcher") } tasks.withType<Test> { useJUnitPlatform() } tasks.test { outputs.dir(project.extra["snippetsDir"]!!) } tasks.asciidoctor { inputs.dir(project.extra["snippetsDir"]!!) dependsOn(tasks.test) }
解决办法
问题核心是测试类用了JUnit 4的@Test注解(导入路径org.junit.Test),但Gradle配置启用了JUnit Platform(useJUnitPlatform()),且项目依赖的是JUnit 5(Spring Boot 3.x默认集成JUnit Jupiter),两者不兼容导致测试无法被识别。
方案1:切换到JUnit 5注解(推荐)
修改测试类的注解导入,替换为JUnit 5的@Test:
// 替换原有@Test的导入语句 import org.junit.jupiter.api.Test;
方案2:兼容JUnit 4(不推荐)
如果要保留JUnit 4的测试代码,需在Gradle中添加JUnit 4兼容依赖:
- 在
dependencies块中追加:
testImplementation("junit:junit:4.13.2") testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
- 保留原有的
tasks.withType<Test> { useJUnitPlatform() }配置,它会自动识别JUnit 4和JUnit 5的测试。
额外检查:测试类需放在src/test/java/com/sample/testing/目录下,Gradle默认只会识别该路径下的测试类。
内容的提问来源于stack exchange,提问作者oldcode
相关产品推荐
相关产品推荐

