微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Apple 芯片无法在 gradle 测试中读取环境变量

如何解决Apple 芯片无法在 gradle 测试中读取环境变量

我正在使用 Apple 芯片(M1 处理器)来开发 sping-boot 应用程序。

我的JVM如下。

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)

我使用的是 gradle-6.8.3-all.zip 版本的 Gradle。

src/test/resources 下,有一个名为 application-test.properties属性文件,其变量如下。

test.environment.variable=${TEST_ENV}

我读取这个变量的测试代码如下。

@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");
    }
}
  • TestServiceTestComponent 分别描述如下。
// 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;
}

我使用下面截图中的方式设置了环境变量。

How I added environment variable.

作为测试结果,我收到以下错误

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}"

我要求 JetBrains 帮助中心解决这个问题,我得到的唯一答案是这不会发生在他们的计算机上。我已经检查过此测试代码是否适用于 Windows 和 Linux。 此外,我曾多次尝试在这台机器(Apple Silicon Macbook Pro)上重新安装 IntelliJ。

示例项目可以在 --> Project Link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。