Groovy trait: 找不到匹配方法错误
Groovy trait: cannot find matching method error
我正在尝试使用 Spock 和 Groovy 进行测试。我写了简单的 class/trait 用于测试如下:
import com.plomber.user.domain.UserDto
import groovy.transform.CompileStatic
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
@CompileStatic
trait SampleUsers {
UserDto John = createDto(1, "john@example.com", "simpleJohn1")
UserDto Bob = createDto(2, "Bob@example.com", "simpleBob1")
static private UserDto createDto(Integer id, String email, String password) {
return UserDto.builder()
.id(id)
.email(email)
.password(new BCryptPasswordEncoder().encode(password))
.build()
}
}
我在尝试编译时遇到以下错误:
Error:(10, 20) Groovyc: [Static type checking] - Cannot find matching method com.plomber.SampleUsers#createDto(int, java.lang.String, java.lang.String). Please check if the declared type is right and if the method exists.
如果我使用 UserDto.builder().id(id) ...
而不是 createDto()
方法分配 John
和 Bob
字段 - 它按预期工作。我错过了什么吗?
来自 groovy 文档:
Traits with static methods cannot be compiled statically or type
checked. All static methods/properties/field are accessed dynamically
(it’s a limitation from the JVM).
我正在尝试使用 Spock 和 Groovy 进行测试。我写了简单的 class/trait 用于测试如下:
import com.plomber.user.domain.UserDto
import groovy.transform.CompileStatic
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
@CompileStatic
trait SampleUsers {
UserDto John = createDto(1, "john@example.com", "simpleJohn1")
UserDto Bob = createDto(2, "Bob@example.com", "simpleBob1")
static private UserDto createDto(Integer id, String email, String password) {
return UserDto.builder()
.id(id)
.email(email)
.password(new BCryptPasswordEncoder().encode(password))
.build()
}
}
我在尝试编译时遇到以下错误:
Error:(10, 20) Groovyc: [Static type checking] - Cannot find matching method com.plomber.SampleUsers#createDto(int, java.lang.String, java.lang.String). Please check if the declared type is right and if the method exists.
如果我使用 UserDto.builder().id(id) ...
而不是 createDto()
方法分配 John
和 Bob
字段 - 它按预期工作。我错过了什么吗?
来自 groovy 文档:
Traits with static methods cannot be compiled statically or type checked. All static methods/properties/field are accessed dynamically (it’s a limitation from the JVM).