在Citrus TestNG测试期间,Spring Boot未加载测试特定应用程序属性

问题描述 投票:0回答:1

我正在尝试让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应用程序的使用,而不是测试包的使用。

java spring spring-boot testng citrus-framework
1个回答
0
投票

你正在调用这两个文件完全相同的东西,application.properties。这意味着只会读取具有最高优先级的单个文件;在这种情况下,它是你的src/test中的一个。相反,使用Spring的配置文件功能将覆盖文件重命名为src/test/resources/application-test.properties,这将允许Boot查看这两个文件并应用所需的覆盖逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.