You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spock测试Spring Boot注册控制器时出现测试用例未找到错误

Spock测试用例找不到的问题排查与解决

我使用Gradle构建Spring Boot项目,引入Spock框架编写AuthenticationController的注册接口测试用例,但运行测试时提示错误:No tests found for given includes: [com.bright.TwitterAnalog.AuthenticationControllerSpec.Register user with valid request]。以下是我的build.gradle文件和测试类代码,请问我哪里操作有误?

原配置与代码

build.gradle

plugins {
    id 'groovy'
    id 'org.springframework.boot' version '3.2.4'
    id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.bright'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '21'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
    implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
    implementation 'org.apache.groovy:groovy'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
//  testImplementation 'org.springframework.boot:spring-boot-starter-test'
//  testImplementation 'org.springframework.security:spring-security-test'
    testImplementation platform("org.spockframework:spock-bom:2.3-groovy-4.0")
    testImplementation "org.spockframework:spock-core"
}

tasks.named('test') {
}

测试类代码

package com.bright.TwitterAnalog

import com.bright.TwitterAnalog.controller.AuthenticationController
import com.bright.TwitterAnalog.dto.UserRegistrationRequest
import com.bright.TwitterAnalog.service.AuthenticationService
import org.springframework.http.ResponseEntity
import org.springframework.http.HttpStatus
import spock.lang.Specification


class AuthenticationControllerSpec extends Specification {
    AuthenticationController authenticationController
    AuthenticationService authenticationService

    def setup() {
        authenticationService = Mock(AuthenticationService)
        authenticationController = new AuthenticationController(authenticationService)
    }

    def "Register user with valid request"() {

        given: "User registration details"
        def request = new UserRegistrationRequest(firstname: "John", lastname: "Doe", username: "johndoe", password: "password")

        authenticationService.registerUser(request) >> ResponseEntity.status(HttpStatus.CREATED).build()

        when: "User is authenticated"
        def response = authenticationController.registerUser(request)

        then: "Is status code is the same as created"
        response.statusCode == HttpStatus.CREATED.value()
    }
}

问题原因与修复方案

1. 配置JUnit平台支持

Spock 2.x基于JUnit 5平台运行,你的test任务未指定使用该平台,导致Gradle无法识别Spock测试用例。修改test任务配置:

tasks.named('test') {
    useJUnitPlatform()
    testLogging {
        events 'PASSED', 'SKIPPED', 'FAILED'
    }
}

2. 检查测试文件路径

Gradle默认Groovy测试类路径为src/test/groovy,确保你的AuthenticationControllerSpec.groovy放在该目录下,而非src/test/java。

3. 补全测试依赖

恢复注释掉的Spring Boot测试依赖,并添加Spock Spring模块(方便后续整合Spring上下文测试):

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation "org.spockframework:spock-spring"

4. 验证测试类规范

你的测试类命名以Spec结尾,符合Spock规范;测试方法命名带空格是Spock支持的写法,无需修改。

修改后的完整build.gradle

plugins {
    id 'groovy'
    id 'org.springframework.boot' version '3.2.4'
    id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.bright'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '21'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
    implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
    implementation 'org.apache.groovy:groovy'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation platform("org.spockframework:spock-bom:2.3-groovy-4.0")
    testImplementation "org.spockframework:spock-core"
    testImplementation "org.spockframework:spock-spring"
}

tasks.named('test') {
    useJUnitPlatform()
    testLogging {
        events 'PASSED', 'SKIPPED', 'FAILED'
    }
}

完成以上修改后,执行./gradlew test命令或在IDE中直接运行测试类,即可正常识别并执行Spock测试用例。

内容的提问来源于stack exchange,提问作者Bright

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 16:16:28