最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - IntelliJ Unit Test Configuration fails with: "variable not initialized in the default constructor" Erro

programmeradmin1浏览0评论

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(";);
    }
}
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

  • Checked Lombok annotation processing: Annotation processing is enabled in IntelliJ (Settings > Build, Execution, Deployment > Compiler > Annotation Processors).
  • Manually added a constructor: If I explicitly define a constructor in MySteps.java, the issue is resolved:
  • Changed the scope for the lombok dependency to compile or left it as the default scope not specified.
  • Tried different versions, both with lombok 1.18.30 and the latest 1.18.36.

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?

Thank you.

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

  • Checked Lombok annotation processing: Annotation processing is enabled in IntelliJ (Settings > Build, Execution, Deployment > Compiler > Annotation Processors).
  • Manually added a constructor: If I explicitly define a constructor in MySteps.java, the issue is resolved:
  • Changed the scope for the lombok dependency to compile or left it as the default scope not specified.
  • Tried different versions, both with lombok 1.18.30 and the latest 1.18.36.

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?

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

Thank you.

Share Improve this question edited Feb 9 at 7:04 Francislainy Campos asked Feb 6 at 7:21 Francislainy CamposFrancislainy Campos 4,1646 gold badges51 silver badges98 bronze badges 8
  • Please, provide screenshot of Settings > Build, Execution, Deployment > Compiler > Annotation Processors. I have cloned your project and everything works fine (test fails, but it is runtime) – Geba Commented Feb 6 at 10:02
  • Sure, the screenshot is added now. – Francislainy Campos Commented Feb 6 at 10:15
  • But could you change it to so-be-automation config (not Default)? – Geba Commented Feb 6 at 10:16
  • If, after reviewing the annotation processing config of so-be-automation, you find that the problem is there, please let me know. I will formulate it as an answer. – Geba Commented Feb 6 at 10:35
  • so-be screenshot is now added. – Francislainy Campos Commented Feb 6 at 10:38
 |  Show 3 more comments

2 Answers 2

Reset to default 1

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

Changing Annotation profile from using Processor path

To using Obtain processor from project classpath.

The problem was in the Processor path - version of Lombok was unknown.
You need to set Processor path to the path of Lombok with the version you are using in your project.
Also no need to have enabled IntelliJ IDEA's Lombok plugin.

For example, I have $HOME/.m2/repository/org/projectlombok/lombok/1.18.30/lombok-1.18.30.jar.

The config so-be-automation/.idea/compiler.xml should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="CompilerConfiguration">
    <annotationProcessing>
      <profile name="Annotation profile for so-be-automation" enabled="true">
        <sourceOutputDir name="target/generated-sources/annotations" />
        <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
        <outputRelativeToContentRoot value="true" />
        <processorPath useClasspath="false">
          <entry name="$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.30/lombok-1.18.30.jar" />
        </processorPath>
        <module name="so-be-automation" />
      </profile>
    </annotationProcessing>
  </component>
  <component name="JavacSettings">
    <option name="ADDITIONAL_OPTIONS_OVERRIDE">
      <module name="so-be-automation" options="-parameters" />
    </option>
  </component>
</project>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论