【发布时间】:2020-05-02 03:01:30
【问题描述】:
在开始之前,在寻找此问题的答案时,我查看了以下未回答我的问题: * Is it possible to add @Builder and @AllArgsConstructor in static inner classes [Lombok]?
在我的测试中,我创建了一个内部类来保存我的测试用例:
@AllArgsConstructor
@Getter
static class TestData {
private final String testCase;
private final String value1;
private final String value2;
private final int value3;
}
同样在这些测试中,我有一个包含所有这些测试用例的列表:
private static final List<TestData> testData = Collections.unmodifiableList(Arrays.asList(
new TestData("test case 1", "value1", "value2", 1),
new TestData("test case 2", "value2", "value2", 2),
new TestData("test case 3", "value2", "value1", 3),
new TestData("test case 4", "value2", "value2", 4),
));
但是,当我编译代码时,出现以下错误:
error: constructor TestData in class TestData cannot be applied to given types;
new TestData("test case 1", "value1", "value2", 1),
^
required: no arguments
found: String,String,String,int
reason: actual and formal argument lists differ in length
编辑 由于它似乎对我的项目不起作用,我附上了我失败的完整示例代码:
public class Testing {
private static final List<TestData> testData = Collections.unmodifiableList(Arrays.asList(
new TestData("test case 1", "value1", "value2", 1),
new TestData("test case 2", "value2", "value2", 2),
new TestData("test case 3", "value2", "value1", 3),
new TestData("test case 4", "value2", "value2", 4)
));
@Test
public void aTest() {
for (final TestData data : testData) {
System.out.println("***********************");
System.out.println(data.getTestCase());
System.out.println(data.getValue1());
System.out.println(data.getValue2());
System.out.println(data.getValue3());
}
}
@AllArgsConstructor
@Getter
static class TestData {
private final String testCase;
private final String value1;
private final String value2;
private final int value3;
}
}
这是我的 build.gradle 文件:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
testCompile 'junit:junit:4.12'
testCompile 'org.projectlombok:lombok:1.18.10'
}
EDIT2 您可以在此 GitHub 存储库中找到我在我的机器(以及我的一些大学)上运行的项目中的代码示例: https://github.com/yonatankarp/stackoverflow_lombok
【问题讨论】:
-
这很奇怪。请放心,我经常使用带有嵌套类的 Lombok 并且那里没有问题。我想,你应该在 github 或类似的地方创建一个完整的项目(包括 gradlew),以便有人可以轻松克隆并自己尝试。
-
感谢您的建议 - 添加了问题的链接
-
在测试代码中使用Lombok注解似乎有问题。我不知道这是 gradle 特定的,还是链接到 IDE。
-
就我而言,添加公共访问说明符使其可访问。似乎 lombok @data 注释仅适用于公共类。但是看看 Gradle 的答案,它似乎与 Maven 插件有关。