无法在我的测试 class 中创建 Lombok class:此处不允许使用静态修饰符
Can't create Lombok class inside my test class: modifier static not allowed here
我想在测试中创建一个 Lombok class class
@RunWith(SpringRunner.class)
@SpringBootTest
public class HostelIntegrationTest {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
@EqualsAndHashCode
class User {
String property1;
Instant property2;
Integer property3;
}
但是我得到这个编译错误:
modifier static not allowed here
将您的内部 class 定义为 static
将解决此问题。
背景:内部class上的每个实例都将引用创建它的外部class的对象,除非内部class 被定义为静态的。通常您不需要该引用,这就是为什么您应该将内部 classes 定义为静态(与静态方法和字段不同,即使从 OOP 的 PoV 来看,这也是一个很好的静态)。
Lombok @Builder
将在您的内部 class (builder()
) 中定义一个静态方法,这只允许在静态内部 classes.
@Builder
使 static
内部 class 内部。
问题可能出在非静态内部class.
里面的静态内部class
尝试让User
也static
//other annotations
@Builder
static class User {
String property1;
Instant property2;
Integer property3;
}
我想在测试中创建一个 Lombok class class
@RunWith(SpringRunner.class)
@SpringBootTest
public class HostelIntegrationTest {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
@EqualsAndHashCode
class User {
String property1;
Instant property2;
Integer property3;
}
但是我得到这个编译错误:
modifier static not allowed here
将您的内部 class 定义为 static
将解决此问题。
背景:内部class上的每个实例都将引用创建它的外部class的对象,除非内部class 被定义为静态的。通常您不需要该引用,这就是为什么您应该将内部 classes 定义为静态(与静态方法和字段不同,即使从 OOP 的 PoV 来看,这也是一个很好的静态)。
Lombok @Builder
将在您的内部 class (builder()
) 中定义一个静态方法,这只允许在静态内部 classes.
@Builder
使 static
内部 class 内部。
问题可能出在非静态内部class.
尝试让User
也static
//other annotations
@Builder
static class User {
String property1;
Instant property2;
Integer property3;
}