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

12,887

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.

Share:
12,887
pc3356
Author by

pc3356

Updated on June 25, 2022

Comments

  • pc3356
    pc3356 almost 2 years

    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)

    • anazimok
      anazimok over 11 years
      Have you ever found a solution for this problem?
  • j0k
    j0k over 11 years
    Please, do not use short link service to inclued an url inside your answer.
  • maxxyme
    maxxyme over 11 years
    OK didn't know that, just copied/pasted the link provided by mhvelplund himself.
  • maxxyme
    maxxyme over 11 years
    In fact, I wasn't aware there was a difference between Maven 3 and Maven 2. According to mojo.codehaus.org/aspectj-maven-plugin/includeExclude.html when using Maven 2 you have to put an empty <source/> element nested in the <sources></sources> element. And the <version>1.4</version> is important too, otherwise the configuration won't work.