我有这个
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') {
}
还有这个认证测试类。我试图用 spock 测试我的注册控制器,但它似乎对我不起作用。
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()
}
}
当我运行测试时,我在标题中收到错误。我做错了什么?
我对使用 gradle builder 的 spock 测试和 springboot 应用程序比较陌生,并且在这里查看了类似的解决方案,但似乎找不到适合我的解决方案
此外,对于 Spring Boot 3,您应该使用 Spock 2.4-M4,您无需将测试任务或测试套件配置为使用 JUnit Platform / Spock。
默认测试套件的测试任务默认配置为使用 JUnit 4,因此找不到基于 JUnit 5 平台的 Spock 测试。
除此之外,我建议您停止使用 Spring 依赖管理插件。这是 Gradle 没有内置 BOM 支持时的过时遗留物,现在弊大于利,甚至该插件的维护者也建议不要再使用它,而是使用内置的 BOM 支持使用
platform(...)
,如 Spring Boot 文档中所述。