Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple silicon not able to read environment variable in gradle test

I am using Apple silicon (M1 Processor) for developing sping-boot application.

My JVM is as below.

openjdk version "1.8.0_282"
OpenJDK Runtime Environment (Zulu 8.52.0.23-CA-macos-aarch64) (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (Zulu 8.52.0.23-CA-macos-aarch64) (build 25.282-b08, mixed mode)

And I am using gradle-6.8.3-all.zip version of Gradle.

Under src/test/resources, there is a property file named application-test.properties and it has variables as below.

test.environment.variable=${TEST_ENV}

My test code for reading this variable is as below.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:application-test.properties")
public class EnvironmentVariableReadTest {

    @Autowired
    private TestService testService;

    @Test
    public void environmentVariableReadTest() {
        String variable = testService.getEnvironmentVariableFromAutowiredComponent();
        assertNotNull(variable);
        System.out.println(variable + " is not null");
    }
}
  • TestService and TestComponent is each described below.
// TestService

@Service
@RequiredArgsConstructor
public class TestService {

    private final TestComponent testComponent;

    public String getEnvironmentVariableFromAutowiredComponent() {
        return testComponent.getEnvironmentVariable();
    }
}

// TestComponent

@Component
@Getter
public class TestComponent {

    @Value("${test.environment.variable}")
    private String environmentVariable;
}

I set the environment variable using the way on the screenshot below.

How I added environment variable. As test result, I am getting the error below.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testComponent': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'TEST_ENV' in value "${TEST_ENV}"

I asked JetBrains help center to resolve this issue, and the only answer I got is that this doesn't happen on their computer. I've checked that this test code works on windows and Linux. Also, I have tried to clean-reinstall IntelliJ several times on this machine (Apple Silicon Macbook Pro).

Sample project can be reproduced at --> Project Link

like image 418
Roy Ra Avatar asked Oct 17 '25 02:10

Roy Ra


2 Answers

I had a very similar problem. Unfortunately in my case, the solution was to use JDK via Rosetta 2 or upgrade Gradle to version 7.x.x.

like image 162
makz. Avatar answered Oct 19 '25 20:10

makz.


Open the Gradle settings:

enter image description here

Change Build and run using and Run and test using to Intellij Idea.

enter image description here

like image 40
B45i Avatar answered Oct 19 '25 20:10

B45i



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!