Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 206
pc3356 Avatar asked May 17 '12 21:05

pc3356


People also ask

What do you have to avoid in tests in unit testing?

Avoid Test Interdependence You, therefore, cannot count on the test suite or the class that you're testing to maintain state in between tests. But that won't always make itself obvious to you. If you have two tests, for instance, the test runner may happen to execute them in the same order each time.

Why is JUnit test ignored?

The @Ignore annotation helps in this scenario. A test method annotated with @Ignore will not be executed. If a test class is annotated with @Ignore, then none of its test methods will be executed.


1 Answers

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.

like image 87
mhvelplund Avatar answered Sep 28 '22 01:09

mhvelplund