简体   繁体   English

似乎无法让 Lombok 在单元测试中工作

[英]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.我们一直在整理一些(非常简单的)代码,以便测试并将 Lombok 注释引入我们的项目,以使我们的代码更好一点。 Unfortunately, seems to break in testing, both through Maven and when the tests are run through IntelliJ.不幸的是,似乎在通过 Maven 和通过 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.我们在 Maven(以及 IntelliJ 中的相关插件)中有 aspectjrt 依赖,以及 aspectj-maven-plugin。

We're running with Maven 2-style POMs, JSDK 1.6.0_31, Lombok 0.11.0:我们正在运行 Maven 2 样式 POM、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.如果我们能实现这个功能那就太好了,因为我已经关注在生产中使用 Lombok 已经有一段时间了。

Many thanks,非常感谢,

P. P。

(FWIW, IntelliJ 11.1.2 has the Lombok plugin 0.4 and seems to be using ACJ for this project) (FWIW,IntelliJ 11.1.2 有 Lombok 插件 0.4 并且似乎在这个项目中使用 ACJ)

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. 问题似乎是lombok生成的代码被ajc覆盖,并且根据Fabrizio Giudici发现的博客条目,由于Maven AspectJ插件中的一个错误阻止你通过,所以没有“干净”的Maven解决方案。 ajc的必要论据。

\n

He proposes a workaround here: http://weblogs.java.net/blog/fabriziogiudici/archive/2011/07/19/making-lombok-aspectj-and-maven-co-exist 他提出了一个解决方法: 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. 您可能必须使用额外的weave目录为测试类添加执行阶段。

Unfortunately, I tested the second solution - mentioned by mhvelplund - but it didn't worked for me. 不幸的是,我测试了第二个解决方案 - 由mhvelplund提到 - 但它对我没用 Solution was to entirely remove the AspectJ maven plugin from the pom.xml! 解决方法是从pom.xml中完全删除AspectJ maven插件!

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:解决方案 - 对于使用 Gradle 的任何人 - 是在build.gradle lombok ,如下所示:

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.确保将 lombok 版本升级到现在可用的最新版本。

What didn't work, is specifying lombok as a dependency;什么不起作用,将 lombok 指定为依赖项; this worked in production code, but not test code:这适用于生产代码,但不适用于测试代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM