【问题标题】:gradle can't find lombok generated constructor in integration testgradle在集成测试中找不到lombok生成的构造函数
【发布时间】:2019-08-21 08:35:53
【问题描述】:

我有带有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 配置有问题?

【问题讨论】:

  • @Data 应该生成 NoArgAllArg 构造函数
  • 我过去曾遇到过 getter 和 setter 注释似乎没有生成它们的情况。我最终只是自己写了它们。这些问题在龙目岛似乎很常见

标签: java gradle lombok integration-testing


【解决方案1】:

我也发现了同样的问题,并通过在 build.gradle 中添加了 annotationProcessor 旁边的 testAnnotationProcessor 来解决:

annotationProcessor "org.projectlombok:lombok:${lombok_version}"
testAnnotationProcessor "org.projectlombok:lombok:${lombok_version}"

【讨论】:

    【解决方案2】:

    我相信您缺少的关键部分是 annotation processor configuration 用于您的 integrationTest 源集:

        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;
      }
    }
    

    【讨论】:

    • 如何在main 中添加类的可见性?因为我无法从主目录访问任何课程。
    • 抱歉回复晚了:除非您已经找到答案,否则我建议为该问题创建一个新问题。谢谢!
    • 我找到了解决办法
    • 现在是 testAnnotationProcessor。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2019-06-20
    • 2019-10-16
    • 1970-01-01
    • 2021-11-29
    • 2021-06-14
    • 2017-06-26
    相关资源
    最近更新 更多