Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok @getter not working in src/test/java package

I have upgraded my jdk from 10 to 11 with all the dependencies updated to use the latest versions. When compiling using gradle wrapper it is throwing following error

symbol: method getId() TestLombok.java:55: error: cannot find symbol object.setId(Long.valueOf(getRandomString(5, onlyNumbers)));

  • Gradle Wrapper Distribution version 5.4.1
  • JDK 11.0.2
  • Mac Machine
  • Lombok 1.18.8

I have tried with various versions of lombok but not able to solve the issue

  • 1.18.8
  • 1.18.4

previously I was using lombok 1.18.2 and annotationprocessor 1.18.2

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {

  @JsonProperty("id")
  public Long id;
}

I expect the issues to be fixed with gradle5.x.x version but still the issue persists. Let me know if we have any issue using gradle wrapper version. Following is my build.gradle file

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://jitpack.io" }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}


apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "org.springframework.boot"

group = "com.demo"
version = "0.0.1-SNAPSHOT"
sourceCompatibility = 11
targetCompatibility = 11

repositories {
    maven { url "https://plugins.gradle.org/m2/" }
    maven { url "http://repo.maven.apache.org/maven2" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url "https://maven.google.com" }
    jcenter()
    mavenCentral()
}

dependencies {
    compile group: 'javax.inject', name: 'javax.inject', version: '1'
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.1.4.RELEASE'
    compile("org.springframework.boot:spring-boot-starter-web")
    compileOnly("org.projectlombok:lombok:1.18.8")
    compile("org.mockito:mockito-core:" + mockitoVersion)
    test.useJUnitPlatform()
    annotationProcessor ('org.projectlombok:lombok:1.18.8')
    testCompileOnly("org.projectlombok:lombok:1.18.8")

}
like image 950
Manish Chandra Ranga Avatar asked Jun 07 '19 06:06

Manish Chandra Ranga


4 Answers

For Lombok annotations in test source sets, you need to add Lombok to two dependency configurations:

testCompileOnly '...'
testAnnotationProcessor '...'

In the same way as compileOnly and annotationProcessor the first one makes the annotations available to the code and the second one activates annotation processing during compilation.

like image 69
Lukas Körfer Avatar answered Sep 22 '22 10:09

Lukas Körfer


I just reproduced the same issue which you faced, my gradle version is

Gradle Version : 5.4.1

To resolve this, in reference to the Lombok doc (https://projectlombok.org/setup/gradle) I changed the dependency as below.

    compileOnly 'org.projectlombok:lombok:1.18.8'
    annotationProcessor 'org.projectlombok:lombok:1.18.8'

You could take look at this https://github.com/rzwitserloot/lombok/issues/1945 for more info

like image 25
Avi Avatar answered Sep 20 '22 10:09

Avi


You didn’t post your build.gradle but I’m guessing you declared Lombok dependency as compileOnly, which only applies to main code. Also declare it as testCompileOnly.

like image 23
Abhijit Sarkar Avatar answered Sep 21 '22 10:09

Abhijit Sarkar


Finally able to solve this issue by upgrading the gradle version to 5.6 and also testCompileOnly '...' testAnnotationProcessor '...'

by adding the above two things in build.gradle file

like image 26
Manish Chandra Ranga Avatar answered Sep 23 '22 10:09

Manish Chandra Ranga