javaspring-bootmavenlombokintellij-lombok-plugin

IntelliJ Unit Test Configuration fails with: "variable not initialized in the default constructor" Error When Using Lombok Annotation


Issue

I'm encountering the following error when running my project as a unit test configuration in IntelliJ:

/Users/francislainycampos/IdeaProjects/so-be-automation/src/test/java/com/francislainy/sobeautomation/steps/MySteps.java:14:30
java: variable restClient not initialized in the default constructor

However, the same tests work fine when I run mvn test from the terminal.

My Setup

RestClient.java
@Slf4j
@Component
@AllArgsConstructor
public class RestClient {
    public RequestSpecification getRequestSpecification() {
        // Rest Assured config here
    }
}
CucumberSpringConfiguration.java
@CucumberContextConfiguration
@SpringBootTest(classes = TestConfig.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CucumberSpringConfiguration {}
TestConfig.java
@ComponentScan(basePackages = {"com.francislainy.sobeautomation"})
@EnableAutoConfiguration
public class TestConfig {}
MySteps.java
@RequiredArgsConstructor
public class MySteps {
    private final RestClient restClient;
    private Response response;

    @Given("I send a GET request to the Bored API")
    public void iSendAGETRequestToTheBoredAPI() {
        response = restClient.getRequestSpecification().get("https://www.boredapi.com/api/activity");
    }
}
pom.xml (Relevant Dependencies)
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

What I've Tried

However, the only workaround I've found so far is to remove the @RequiredArgsAnnotation and explicitly define a constructor for my variable.

public class MySteps {
    private final RestClient restClient;
    private Response response;
    
    public MySteps(RestClient restClient) {
        this.restClient = restClient;
    }
}

Question

Why does @RequiredArgsConstructor fail to initialize restClient when running the tests in IntelliJ's unit test configuration, but works fine with mvn test? Is there a way to ensure the Lombok-generated constructor works correctly in both scenarios?

enter image description here

enter image description here

https://github.com/francislainy/so-be-automation

Thank you.


Solution

  • Based on the conversation with @Geba, this is what has fixed the issue.

    Changing Annotation profile from using Processor path

    enter image description here

    To using Obtain processor from project classpath.

    enter image description here