Pio*_*ski 64 java spring spring-boot
Spring-boot使用Spring配置文件(http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html),它允许例如为不同的环境分别配置.我使用此功能的一种方法是配置测试数据库以供集成测试使用.我想知道是否有必要创建我自己的配置文件'test'并在每个测试文件中明确激活此配置文件?现在我用以下方式做到:
在每个测试文件中包括:
@ActiveProfiles("test")
Run Code Online (Sandbox Code Playgroud)有更聪明/更简洁的方式吗?例如,默认测试配置文件?
编辑1:这个问题与Spring-Boot 1.4.1有关
Mat*_*nkt 74
据我所知,没有任何内容可以直接解决您的请求 - 但我可以建议一个可以提供帮助的提案:
您可以使用自己的测试注释,这是一个包含和的元注释.因此,您仍然需要专用配置文件,但避免在所有测试中分散配置文件定义.@SpringBootTest@ActiveProfiles("test")
此注释将默认为配置文件test,您可以使用元注释覆盖配置文件.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ActiveProfiles
public @interface MyApplicationTest {
@AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"test"};
}
Run Code Online (Sandbox Code Playgroud)
Pie*_*nry 42
另一种方法是定义实际测试类将扩展的基类(抽象)测试类:
@RunWith(SpringRunner.class)
@SpringBootTest()
@ActiveProfiles("staging")
public abstract class BaseIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)
具体测试:
public class SampleSearchServiceTest extends BaseIntegrationTest{
@Inject
private SampleSearchService service;
@Test
public void shouldInjectService(){
assertThat(this.service).isNotNull();
}
}
Run Code Online (Sandbox Code Playgroud)
这允许您提取的不仅仅是@ActiveProfiles注释.您还可以想象针对不同类型的集成测试的更专业的基类,例如数据访问层与服务层,或功能专业(常见@Before或@After方法等).
小智 31
您可以将application.properties文件放在test/resources文件夹中.在那里你设置
spring.profiles.active=test
Run Code Online (Sandbox Code Playgroud)
这是运行测试时的默认测试配置文件.
Boj*_*vic 12
如果你使用 maven,你可以在 pom.xml 中添加:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>-Dspring.profiles.active=test</argLine>
</configuration>
</plugin>
...
Run Code Online (Sandbox Code Playgroud)
然后,maven 应该使用这个参数运行你的集成测试(*IT.java),并且 IntelliJ 也将从这个配置文件激活开始 - 这样你就可以在里面指定所有属性
application-test.yml
Run Code Online (Sandbox Code Playgroud)
并且您不需要“-default”属性。
要激活“测试”配置文件,请在 build.gradle 中写入:
test.doFirst {
systemProperty 'spring.profiles.active', 'test'
activeProfiles = 'test'
}
Run Code Online (Sandbox Code Playgroud)
小智 7
这样做的一种相对的方式(实际上,距离@Compito的原始答案还不到一个星期):
spring.profiles.active=test为test/resources/application-default.properties。test/resources/application-test.properties测试并仅覆盖所需的属性。小智 7
您可以将测试特定属性放入src/test/resources/config/application.properties.
此文件中定义的属性将覆盖src/main/resources/application.properties测试期间定义的属性。
有关为何如此有效的更多信息,请查看Spring Boots 文档。
到 2021 年和 Spring Boot 2.4,我发现的解决方案是有 3 个属性文件
src/main/resources/application.yml - 包含应用程序的默认道具src/test/resources/application.yml - 将配置文件设置为“test”,并从“main”导入属性src/test/resources/application-test.yml - 包含特定于测试的配置文件,它将覆盖“主要”以下是内容src/test/resources/application.yml:
# for testing, set default profile to 'test'
spring.profiles.active: "test"
# and import the 'main' properties
spring.config.import: file:src/main/resources/application.yml
Run Code Online (Sandbox Code Playgroud)
例如,如果src/main/resources/application.yml有内容
ip-address: "10.7.0.1"
username: admin
Run Code Online (Sandbox Code Playgroud)
并且src/test/resources/application-test.yml有
ip-address: "999.999.999.999"
run-integration-test: true
Run Code Online (Sandbox Code Playgroud)
然后(假设没有其他配置文件)...
运行测试时,
profiles=test
--
ip-address=999.999.999.999
username=admin
run-integration-test=true
Run Code Online (Sandbox Code Playgroud)
并且在正常运行应用程序时
profiles=none
--
ip-address=10.7.0.1
username=admin
run-integration-test <undefined>
Run Code Online (Sandbox Code Playgroud)
注意:如果src/main/resources/application.ymlcontains spring.profiles.active: "dev",则不会被覆盖src/test/resources/application-test.yml
就我而言,根据环境,我有不同的 application.properties,例如:
application.properties (base file)
application-dev.properties
application-qa.properties
application-prod.properties
Run Code Online (Sandbox Code Playgroud)
和 application.properties 包含一个属性 spring.profiles.active 来选择正确的文件。
对于我的集成测试,我在application-test.properties里面创建了一个新文件,test/resources并带有@TestPropertySource({ "/application-test.properties" })注释,这是负责选择 application.properties 我想要的文件,具体取决于我对这些测试的需求
| 归档时间: |
|
| 查看次数: |
69705 次 |
| 最近记录: |