Groovy特征:找不到匹配方法错误 [英] Groovy trait: cannot find matching method error
本文介绍了Groovy特征:找不到匹配方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Spock和Groovy进行测试.我编写了简单的类/特征进行测试,如下所示:
I'm trying to use Spock and Groovy for testing. I have written simple class/trait for testing as follows:
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()
}
}
尝试编译时出现以下错误:
I'm getting following error when I try to compile it:
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)...
而不是分配
方法-可以正常工作.我想念什么吗? John
和 Bob
字段createDto()
If I assign John
and Bob
fields using UserDto.builder().id(id) ...
instead of createDto()
method - it works as expected. Do I miss something?
推荐答案
来自groovy文档:
From groovy docs:
具有静态方法的特性不能静态编译或键入检查.动态访问所有静态方法/属性/字段(这是JVM的限制).
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).
这篇关于Groovy特征:找不到匹配方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文