gradle 在集成测试中找不到 lombok 生成的构造函数
gradle can't find lombok generated constructor in integration test
我有集成测试,内部 class 带有 lombok
注释。看起来像这样
@Test(dataProvider = "jsonDiff")
public class JaversDiffIntegrationTest {
public void shouldCompareEntities(Person input1, Person input2, String expectedJson)
throws JSONException {
Person p1 = new Person("p_id", "Jack");
Person p2 = new Person("p_id", "Michael");
....
}
@TypeName("TestEntityPerson")
@Data
@AllArgsConstructor
private class Person {
private String id;
private String name;
}
在Idea
我启用了注解处理,至少能编译。当我尝试通过 gradlew
运行 clean build
我得到错误
constructor Person in class JaversDiffIntegrationTest.Person cannot be applied to given types;
Person p2 = new Person("p_id", "Michael");
^
required: no arguments
found: String,String
它似乎没有看到 lombok
生成的构造函数。
我的 build.gradle
看起来像这样(我使用 gradle5
)
apply plugin: 'idea'
// TODO: move to integration-test.gradle
sourceSets {
integrationTest {
java.srcDir 'src/testInteg/java'
resources.srcDir 'src/testInteg/resources'
}
}
configurations {
integrationTestImplementation.extendsFrom implementation
integrationTestRuntimeOnly.extendsFrom runtimeOnly
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath + sourceSets.test.runtimeClasspath
outputs.upToDateWhen { false }
mustRunAfter test
useTestNG() {
suites 'src/testInteg/resources/testng.xml'
}
testLogging {
showStandardStreams = true
}
}
check.dependsOn integrationTest
dependencies {
implementation "javax.validation:validation-api:1.1.0.Final"
testImplementation "junit:junit:4.11"
testImplementation "org.spockframework:spock-core:1.3-groovy-2.5"
testImplementation "org.codehaus.groovy:groovy-all:2.5.6"
implementation "org.javers:javers-core:5.3.2"
annotationProcessor "org.projectlombok:lombok:1.18.6"
implementation "org.projectlombok:lombok:1.18.6"
integrationTestImplementation "org.testng:testng:6.14.3"
integrationTestImplementation "org.skyscreamer:jsonassert:1.5.0"
integrationTestImplementation "com.google.code.gson:gson:2.8.5"
integrationTestImplementation "commons-io:commons-io:2.6"
}
问题是什么?也许我的 integrationTest
配置有问题?
我认为您缺少的关键部分是 integrationTest
源集的 annotation processor configuration:
integrationTestAnnotationProcessor "org.projectlombok:lombok:1.18.6"
在下文中,您可以找到一个独立的工作示例(使用 Gradle 5.3.1 测试)。这不完全是您的项目,但应该足够接近以使您步入正轨:
build.gradle
apply plugin: 'java'
sourceSets {
integrationTest {
java.srcDir 'src/testInteg/java'
resources.srcDir 'src/testInteg/resources'
}
}
configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntimeOnly.extendsFrom testRuntimeOnly
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath + sourceSets.test.runtimeClasspath
}
repositories {
jcenter();
}
dependencies {
implementation "org.projectlombok:lombok:1.18.6"
testImplementation "junit:junit:4.11"
integrationTestAnnotationProcessor "org.projectlombok:lombok:1.18.6"
}
src/testInteg/java/MyTest.java
public class MyTest {
@org.junit.Test
public void test() {
new Person("foo", "bar");
assert true;
}
@lombok.AllArgsConstructor
private class Person {
private String id;
private String name;
}
}
我也发现了同样的问题,并通过在 annotationProcessor 旁边添加 testAnnotationProcessor 来解决 build.gradle:
annotationProcessor "org.projectlombok:lombok:${lombok_version}"
testAnnotationProcessor "org.projectlombok:lombok:${lombok_version}"
就我而言,我还需要添加以下内容:
annotationProcessorPath += main.output + test.output
annotationProcessorPath += sourceSets.test.runtimeClasspath
到此部分:
sourceSets {
integrationTest {
//here
}
}
我有集成测试,内部 class 带有 lombok
注释。看起来像这样
@Test(dataProvider = "jsonDiff")
public class JaversDiffIntegrationTest {
public void shouldCompareEntities(Person input1, Person input2, String expectedJson)
throws JSONException {
Person p1 = new Person("p_id", "Jack");
Person p2 = new Person("p_id", "Michael");
....
}
@TypeName("TestEntityPerson")
@Data
@AllArgsConstructor
private class Person {
private String id;
private String name;
}
在Idea
我启用了注解处理,至少能编译。当我尝试通过 gradlew
运行 clean build
我得到错误
constructor Person in class JaversDiffIntegrationTest.Person cannot be applied to given types;
Person p2 = new Person("p_id", "Michael");
^
required: no arguments
found: String,String
它似乎没有看到 lombok
生成的构造函数。
我的 build.gradle
看起来像这样(我使用 gradle5
)
apply plugin: 'idea'
// TODO: move to integration-test.gradle
sourceSets {
integrationTest {
java.srcDir 'src/testInteg/java'
resources.srcDir 'src/testInteg/resources'
}
}
configurations {
integrationTestImplementation.extendsFrom implementation
integrationTestRuntimeOnly.extendsFrom runtimeOnly
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath + sourceSets.test.runtimeClasspath
outputs.upToDateWhen { false }
mustRunAfter test
useTestNG() {
suites 'src/testInteg/resources/testng.xml'
}
testLogging {
showStandardStreams = true
}
}
check.dependsOn integrationTest
dependencies {
implementation "javax.validation:validation-api:1.1.0.Final"
testImplementation "junit:junit:4.11"
testImplementation "org.spockframework:spock-core:1.3-groovy-2.5"
testImplementation "org.codehaus.groovy:groovy-all:2.5.6"
implementation "org.javers:javers-core:5.3.2"
annotationProcessor "org.projectlombok:lombok:1.18.6"
implementation "org.projectlombok:lombok:1.18.6"
integrationTestImplementation "org.testng:testng:6.14.3"
integrationTestImplementation "org.skyscreamer:jsonassert:1.5.0"
integrationTestImplementation "com.google.code.gson:gson:2.8.5"
integrationTestImplementation "commons-io:commons-io:2.6"
}
问题是什么?也许我的 integrationTest
配置有问题?
我认为您缺少的关键部分是 integrationTest
源集的 annotation processor configuration:
integrationTestAnnotationProcessor "org.projectlombok:lombok:1.18.6"
在下文中,您可以找到一个独立的工作示例(使用 Gradle 5.3.1 测试)。这不完全是您的项目,但应该足够接近以使您步入正轨:
build.gradle
apply plugin: 'java'
sourceSets {
integrationTest {
java.srcDir 'src/testInteg/java'
resources.srcDir 'src/testInteg/resources'
}
}
configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntimeOnly.extendsFrom testRuntimeOnly
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath + sourceSets.test.runtimeClasspath
}
repositories {
jcenter();
}
dependencies {
implementation "org.projectlombok:lombok:1.18.6"
testImplementation "junit:junit:4.11"
integrationTestAnnotationProcessor "org.projectlombok:lombok:1.18.6"
}
src/testInteg/java/MyTest.java
public class MyTest {
@org.junit.Test
public void test() {
new Person("foo", "bar");
assert true;
}
@lombok.AllArgsConstructor
private class Person {
private String id;
private String name;
}
}
我也发现了同样的问题,并通过在 annotationProcessor 旁边添加 testAnnotationProcessor 来解决 build.gradle:
annotationProcessor "org.projectlombok:lombok:${lombok_version}"
testAnnotationProcessor "org.projectlombok:lombok:${lombok_version}"
就我而言,我还需要添加以下内容:
annotationProcessorPath += main.output + test.output
annotationProcessorPath += sourceSets.test.runtimeClasspath
到此部分:
sourceSets {
integrationTest {
//here
}
}