Java 每个位置具有不同属性的Springboot测试
简言之,我的问题是: 如何在不同的环境中运行具有不同属性的测试 长版本: 我编写了一个JUnit测试,该测试在类顶部进行注释,如下所示:Java 每个位置具有不同属性的Springboot测试,java,spring,spring-boot,properties,Java,Spring,Spring Boot,Properties,简言之,我的问题是: 如何在不同的环境中运行具有不同属性的测试 长版本: 我编写了一个JUnit测试,该测试在类顶部进行注释,如下所示: @RunWith(SpringRunner.class) @SpringBootTest( classes=RunServer.class, webEnvironment = WebEnvironment.DEFINED_PORT ) @TestPropertySource(locations="file:conf/application-junit.prop
@RunWith(SpringRunner.class)
@SpringBootTest( classes=RunServer.class, webEnvironment = WebEnvironment.DEFINED_PORT )
@TestPropertySource(locations="file:conf/application-junit.properties")
public class MyDbTest {...
在当前的conf/application.junit.properties中,存在端口5400上的数据库连接语句
但是现在我发现我不仅在本地运行测试,而且在另一台机器上运行测试,比如Jenkins,它的数据库端口位于5500
我有一个带有正确端口的属性文件,它位于conf/application jenkins.properties
现在我想知道如何在源代码中为@TestPropertySource(locations=…)设置正确的值
顺便说一下。这对于非测试代码执行来说完全没有问题,因为我只将java-jar myjar.jar--spring.config.location=我的属性文件不在类路径中。它们被设置为绝对文件路径
谢谢大家的帮助。您不应指定:
@TestPropertySource(locations="file:conf/application-junit.properties")
在测试类中对junit
Spring引导配置文件进行硬编码
相反,您应该根据在执行mvn测试时设置的配置文件执行测试,例如:
mvn clean test -Dspring.profiles.active=junit
mvn clean test -Dspring.profiles.active=jenkins
application junit.properties
或application jenkins.properties
将根据spring.profiles.active
值集自动用于创建应用程序上下文。查看在这种情况下是否可以使用spring.profiles.include
。只是一种预感。我没有亲自尝试过……仅仅两年后。。。一些人认为:我可以激活多个配置文件,不是吗?那么将使用哪些属性呢?