pc3356
pc3356

Reputation: 191

Can't seem to get Lombok to work in unit tests

We've been putting together some (really simple) code in order to test out and introduce Lombok annotations into our project to make our code a bit nicer. Unfortunately, seems to break in testing, both through Maven and when the tests are run through IntelliJ.

Our domain classes look something like:

package foo.bar;

import lombok.Data;

@Data
public class Noddy {

    private int id;
    private String name;

}

With a corresponding test:

package foo.bar;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class NoddyTest {

    @Test
    public void testLombokAnnotations(){
        Noddy noddy = new Noddy();
        noddy.setId(1);
        noddy.setName("some name");
        assertEquals(noddy.getName(), "some name");
    }
}

We have the aspectjrt dependency in Maven (as well as the relevant plugin in IntelliJ), and the aspectj-maven-plugin.

We're running with Maven 2-style POMs, JSDK 1.6.0_31, Lombok 0.11.0:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>0.11.0</version>
</dependency>

Are we doing something stupid or missing something obvious?

It would be great if we can get this working, as I've had an eye to using Lombok in production for some time now.

Many thanks,

P.

(FWIW, IntelliJ 11.1.2 has the Lombok plugin 0.4 and seems to be using ACJ for this project)

Upvotes: 17

Views: 15382

Answers (3)

nightblade9
nightblade9

Reputation: 415

While this question is now ten years old (!), I ran into this problem, too.

The solution - for anyone using Gradle - is to specify the lombok plugin in build.gradle, like so:

plugins {
  // spring, boot, java, etc.
  id 'io.freefair.lombok' version '6.5.0.3'
}

Make sure you upgrade the lombok version to whatever's the latest version available now.

What didn't work, is specifying lombok as a dependency; this worked in production code, but not test code:

dependencies {
  // spring, boot, etc.
  compileOnly 'org.projectlombok:lombok:1.18.24'
}

Upvotes: 0

mhvelplund
mhvelplund

Reputation: 2341

The problem seems to be that the lombok generated code is overwritten by ajc, and according to a blog entry I found by Fabrizio Giudici, there is no "clean" Maven solution due to a bug in the Maven AspectJ plugin that prevents you from passing the necessary arguments to ajc.

He proposes a workaround here: http://weblogs.java.net/blog/fabriziogiudici/archive/2011/07/19/making-lombok-aspectj-and-maven-co-exist

Actually, this worked for me, and it's arguably a cleaner solution. You might have to add an execution phase for the test classes with an additional weave directory.

Upvotes: 1

maxxyme
maxxyme

Reputation: 2334

Unfortunately, I tested the second solution - mentioned by mhvelplund - but it didn't worked for me. Solution was to entirely remove the AspectJ maven plugin from the pom.xml!

Upvotes: 0

Related Questions