Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit not working with Lombok - annotation processing doesn't seem to work for test classes

I have a problem with Lombok and JUnit.

I am using IntelliJ Idea, the latest one, with Lombok plugin installed and annotation processing enabled.

I have an entity class:

@Data
@Builder
@AllArgsConstructor
public class User {

    private String name;
    private String email;

}

build.gradle:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
    testCompile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
    testAnnotationProcessor "org.projectlombok:lombok:1.18.10"
}

And finally, the test case:

@Test
public void whenCheckIfPresent_thenOk() {
    User user = User.builder().name("John").email("[email protected]").build();
    Optional<User> opt = Optional.ofNullable(user);
    assertTrue(opt.isPresent());

    assertEquals(user.getEmail(), opt.get().getEmail());
}

When I am trying to run this test I get below error:

IdeaProjects\Tutoriale\src\test\java\optionals\OptionalsTest.java:26: error: cannot find symbol
        User user = User.builder().name("John").email("[email protected]").build();
                        ^
  symbol:   method builder()
  location: class User
IdeaProjects\Tutoriale\src\test\java\optionals\OptionalsTest.java:30: error: cannot find symbol
        assertEquals(user.getEmail(), opt.get().getEmail());
                         ^
  symbol:   method getEmail()
  location: variable user of type User
IdeaProjects\Tutoriale\src\test\java\optionals\OptionalsTest.java:30: error: cannot find symbol
        assertEquals(user.getEmail(), opt.get().getEmail());
                                               ^
  symbol:   method getEmail()
  location: class User

It seems that annotation processing isn't working, but I have no idea how to fix this.

like image 740
Zbigniew Nitecki Avatar asked Dec 20 '19 14:12

Zbigniew Nitecki


People also ask

How do I enable annotation processing on Lombok?

We need to go to the Preferences | Build, Execution, Deployment | Compiler | Annotation Processors and make sure of the following: Enable annotation processing box is checked.

How do I enable annotation processing in Lombok IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Compiler | Annotation Processors.

What is the annotation required for JUnit test methods?

A JUnit test is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation. This method executes the code under test.

Is Lombok annotation processor?

With javac (and netbeans, maven, gradle, and most other build systems), lombok runs as an annotation processor.


2 Answers

What Gradle version are you using? This issue was seen in Gradle version 5.4.1.

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

You could try the dependency as below,

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

Reference: https://projectlombok.org/setup/gradleLombok doc

More information can be found in the below location,

https://github.com/rzwitserloot/lombok/issues/1945

like image 140
Sreedhar S Avatar answered Sep 18 '22 03:09

Sreedhar S


You've used incorrect dependency testAnnotationProcessor it should be annotationProcessor, see lombock-gradle doc. After fixing gradle file you'll be able to build it with terminal. Also it could be still failed in IDE because of lombok requires to enable annotation processing for IDE (check your IDE documentation).

like image 20
Sergii Avatar answered Sep 20 '22 03:09

Sergii