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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10643936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:56:59  来源:igfitidea点击:

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

javaunit-testingjunitintellij-idealombok

提问by pc3356

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.

我们已经将一些(非常简单的)代码放在一起,以便测试并将 Lombok 注释引入我们的项目,以使我们的代码更好一些。不幸的是,通过 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 中有 aspectjrt 依赖(以及 IntelliJ 中的相关插件)和 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)

回答by mhvelplund

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 的必要参数。

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, thisworked 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.

实际上,对我有用,而且可以说是一种更清洁的解决方案。您可能需要使用额外的编织目录为测试类添加一个执行阶段。

回答by maxxyme

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!

不幸的是,我测试了mhvelplund 提到的第二个解决方案,但它对我不起作用。解决方案是从 pom.xml 中完全删除 AspectJ maven 插件!