Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create Lombok class inside my test class: modifier static not allowed here

Tags:

java

lombok

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

like image 761
Sandro Rey Avatar asked Jan 17 '20 10:01

Sandro Rey


2 Answers

@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;
}
like image 194
dehasi Avatar answered Oct 12 '22 14:10

dehasi


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.

like image 26
Nicktar Avatar answered Oct 12 '22 14:10

Nicktar