I want to create a Lombok class inside a test class
@RunWith(SpringRunner.class)
@SpringBootTest
public class HostelIntegrationTest {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
@EqualsAndHashCode
class User {
String property1;
Instant property2;
Integer property3;
}
but I get this compilation error:
modifier static not allowed here
@Builder
makes a static
internal class inside.
The problem is probably the static internal class inside the non-static internal class.
Try to make User
also static
//other annotations
@Builder
static class User {
String property1;
Instant property2;
Integer property3;
}
Defining your inner class as static
will solve this.
Background: every instance on an inner class will have a reference to the object of the outer class that created it, unless the inner class ist defined as static. Usually you will not need that reference, that's why you should define your inner classes as static (this is a good static even from the PoV of OOP unlike static methods and fields).
Lombok @Builder
will define a static method in your inner class (builder()
), that's only allowed in static inner classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With