Groovy Builder转换真的有效吗?
我尝试在以下代码中使用转换:Groovy Builder转换真的有效吗?,groovy,builder,Groovy,Builder,我尝试在以下代码中使用转换: plugins { id 'groovy' id 'java' } group 'org.example' version '1.0-SNAPSHOT' sourceCompatibility = 11 repositories { mavenCentral() } dependencies { implementation 'org.codehaus.groovy:groovy-all:2.4.15' testCo
plugins {
id 'groovy'
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:2.4.15'
testCompile group: 'junit', name: 'junit', version: '4.12'
testImplementation ('org.spockframework:spock-core:1.1-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
}
}
错误:
package pattern
import spock.lang.Specification
class HttpRequestBuilderAnnotationSpec extends Specification {
private String url = 'https://dsd.com'
void "Instantiate HttpRequest"() {
when:
HttpRequestBuilderAnnotation httpRequestBuilderAnnotation = HttpRequestBuilderAnnotation.builder()
.url(url).build()
then:
assert httpRequestBuilderAnnotation
assert httpRequestBuilderAnnotation.getUrl() == url
}
}
----------注:
如果删除了“private”修饰符,代码就可以运行了,但我相信根据Groovy规范,private修饰符应该按原样工作。这是否回答了您的问题?不是真的。我只是尝试将生成器转换用于一个普通的Fluent API风格的方法,但是您使用的是带有不可变(final)字段的默认策略。
package pattern
import spock.lang.Specification
class HttpRequestBuilderAnnotationSpec extends Specification {
private String url = 'https://dsd.com'
void "Instantiate HttpRequest"() {
when:
HttpRequestBuilderAnnotation httpRequestBuilderAnnotation = HttpRequestBuilderAnnotation.builder()
.url(url).build()
then:
assert httpRequestBuilderAnnotation
assert httpRequestBuilderAnnotation.getUrl() == url
}
}
groovy.lang.MissingMethodException: No signature of method: static pattern.HttpRequestBuilderAnnotation.url() is applicable for argument types: (java.lang.String) values: [https://dsd.com]
Possible solutions: use([Ljava.lang.Object;), grep(), dump(), any(), getUrl(), use(java.lang.Class, groovy.lang.Closure)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at pattern.HttpRequestBuilderAnnotationSpec.Instantiate HttpRequest(HttpRequestBuilderAnnotationSpec.groovy:12)