Spring 引导:在测试中引用来自 application.properties 的 pom 属性
Spring boot: reference pom properties from application.properties in tests
这是我的src/test/resources/application.yml:
app:
name: Configuration And Profiles
desc: This is an app to try out ${app.name}
version: @modelVersion@
我只是想从我的 pom 文件中获取模型版本:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
...
测试:
@WebMvcTest(ProfilingController.class)
public class RestControllerTest {
@Value("${app.version}")
private String version;
@Test
public void testHelloEnv() throws Exception {
System.out.println("VERSION: " + version);
}
}
我得到的错误:
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
in 'reader', line 4, column 12:
version: @modelVersion@
^
有趣的是,我在 src/main/resource/application.yml 中得到了相同的 属性,但在那种情况下,当我 运行 应用程序正常,版本:@modelVersion 是从 pom 正确填充的,我可以毫无问题地获取版本。就像测试的 application.yml 不知道 pom.xml
有什么想法吗?
注意:在单引号之间使用“@modelVersion”可以防止错误,但我得到的是字符串@modelVersion 而不是它的值 4.0.0
技术:
- Spring开机版本:2.5.6
- Junit 版本:Jupiter (5)
- Maven 版本:3.8.3
如 documentation 中所述,资源过滤由 spring-boot-starter-parent
启用
'test' 属性不是这种情况,所以如果您需要这个,
您需要在测试资源中启用过滤:
在你的 pom.xml
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
...
</build>
这是我的src/test/resources/application.yml:
app:
name: Configuration And Profiles
desc: This is an app to try out ${app.name}
version: @modelVersion@
我只是想从我的 pom 文件中获取模型版本:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
...
测试:
@WebMvcTest(ProfilingController.class)
public class RestControllerTest {
@Value("${app.version}")
private String version;
@Test
public void testHelloEnv() throws Exception {
System.out.println("VERSION: " + version);
}
}
我得到的错误:
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
in 'reader', line 4, column 12:
version: @modelVersion@
^
有趣的是,我在 src/main/resource/application.yml 中得到了相同的 属性,但在那种情况下,当我 运行 应用程序正常,版本:@modelVersion 是从 pom 正确填充的,我可以毫无问题地获取版本。就像测试的 application.yml 不知道 pom.xml
有什么想法吗?
注意:在单引号之间使用“@modelVersion”可以防止错误,但我得到的是字符串@modelVersion 而不是它的值 4.0.0
技术:
- Spring开机版本:2.5.6
- Junit 版本:Jupiter (5)
- Maven 版本:3.8.3
如 documentation 中所述,资源过滤由 spring-boot-starter-parent
'test' 属性不是这种情况,所以如果您需要这个, 您需要在测试资源中启用过滤:
在你的 pom.xml
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
...
</build>