我正在尝试让Spring Boot在运行我的Citrus集成测试时自动加载src/test/resources/application.properties
文件而不是src/main/resources/application.properties
文件。这些属性在测试期间可用,但它们不会覆盖主Spring Boot应用程序中使用的主要属性。
这是我到目前为止的配置:
的src /主/爪哇/ EventPublisher.java
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
public class EventPublisher {
public static void main(String[] args) {
SpringApplication.run(EventPublisher.class, args);
}
}
SRC /主/资源/ application.properties
consumer.to=stream:out
的src /测试/ JAVA / EndpointConfig.java
@Configuration
@PropertySource("classpath:application.properties")
@SpringBootTest
public class EndpointConfig {
...
的src /测试/ JAVA / CitrusTestsIT.java
@Test
@SpringBootTest
public class AutomatedIT extends TestNGCitrusTestRunner {
...
SRC /测试/资源/ application.properties
citrus.rest.server.port=7913
consumer.to=localhost:${citrus.rest.server.port}/consumer
pom.xml spring片段
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我假设有一个注释或文件结构问题,但到目前为止我还没能弄清楚那个问题是什么。
更新
在将文件重命名为src/main/resources
并更改application-test.properties
中的spring-boot
插件后,我从pom.xml
加载测试属性:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<fork>true</fork>
<profiles>test</profiles>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
通过在POM中将配置文件设置为test
,现在Spring Boot加载测试属性。
这似乎不是正确的方法,这也限制了application-test.properties
文件到主Spring Boot应用程序的使用,而不是测试包的使用。
你正在调用这两个文件完全相同的东西,application.properties
。这意味着只会读取具有最高优先级的单个文件;在这种情况下,它是你的src/test
中的一个。相反,使用Spring的配置文件功能将覆盖文件重命名为src/test/resources/application-test.properties
,这将允许Boot查看这两个文件并应用所需的覆盖逻辑。