![](/img/trans.png)
[英]Inject / override a properties value to a Spring Boot properties file in unit/integration tests phase
[英]Spring Boot: How to override default properties in Unit tests
我尝试为我的单元测试加载第二个属性文件,这将覆盖一些属性。
在@Configuration
上使用@PropertySource
加载它不起作用,使用@TestPropertySource
加载它也不起作用。 只能直接在@TesPropertySource
上设置properties
,但是当我尝试将其变成元注释时,它不起作用。
这是一个示例项目: https : //github.com/cptwunderlich/SpringTestProperties
我更喜欢加载一个文件影响所有测试(例如使用@PropertySource
),但如果这不起作用,至少有一个自定义元注释会很好,所以我不必把它放在每个和每一次测试。 基本上我想一些数据不能导入到数据库的测试(spring.datasource.data)后来也改变使用的数据库-而不复制整个配置,不得不改变它在每次两个地方。
重要的一点:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
public class TestconfigApplicationTests {
@Value("${my.test.property}")
private String testproperty;
@Test
public void assertValue() {
Assert.assertEquals("foobar", testproperty);
}
}
或者, 测试包中的config类:
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@PropertySource("classpath:application-test.properties")
public class GlobalTestConfig {
}
更新:
答案中的主要建议是使用@ActiveProfile
激活'test'配置文件,这将导致加载'application-test.yaml'。 这比@TestPropertySource
,但我仍然需要在每个Test Class上添加一个注释。 我尝试创建一个元注释 - 它应该工作 - 所以至少我只有一个自定义注释,我可以捆绑其他设置。 但这不起作用。
完美的解决方案是使用一个配置类全局设置这些设置,而不必在每个测试上添加注释。 我仍然在寻找这个解决方案,或者至少在关闭这个问题之前调试元注释。
编辑:我创建了一个Jira问题:SPR-17531
编辑
好吧,我有点困惑,所以我重新测试了所有不同的组合:
@TestPropertySource(locations = "classpath:application-test.properties")
上的测试,实际上现在的作品 。 呵呵。 @ActiveProfiles("test")
。 @ActiveProfiles
不起作用 。 编辑:它确实...... 编辑:
好的,我的错误。 元注释可行 - 我忘了设置保留策略,默认为CLASS。 添加@Retention(RUNTIME)
修复了这个问题。
似乎没有办法在代码中全局设置它(即,没有在我的IDE中配置如何运行测试),所以我现在必须使用该配置文件。
您可以使用@ActiveProfiles("test")
。 这会将application-test.yml
属性设置为测试环境。
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class TestconfigApplicationTests {
...
}
如果我们需要针对不同的环境,那么在Boot中有一个内置机制,因此不需要额外的库或重构。
我们可以简单地在src/main/resources
目录中定义application-environment.properties
文件 - 然后使用相同的环境名称设置Spring配置文件。
例如,如果我们定义一个staging
或test
环境,那意味着我们必须定义一个临时或测试配置文件,然后定义application-staging.properties
或application-test.properties
。
此env
文件将被加载,并将优先于默认属性文件application.properties
。 请注意,仍然会加载默认文件,只是当存在属性冲突时,环境特定属性文件优先,这意味着application-staging.properties
或application-test.properties
指定的属性将覆盖application-staging.properties
的属性application.properties
。
每个测试类都使用自己的配置文件,因此您需要为每个类指定活动配置文件。
您可能感兴趣的另一件事是您可以通过configuration
类模拟服务
@Configuration
@Profile("mockEntityService")
public class EntityServiceMockProvider {
@Bean
@Primary
public EntityService entityService() {
EntityService mockedEntityService = Mockito.mock(EntityService.class);
Entity entity= Mockito.mock(Entity.class);
when(mockedEntityService.save(any(Entity.class)))
.thenReturn(entity);
return mockedEntityService ;
}
}
在测试类中,您可以使用多个活动配置文件:例如@ActiveProfiles({"test", "mockEntityService"})
因此,您将使用EntityService
实现,而不是使用EntityService
的实际实现。
您可以将application.properties添加到
src/test/resources
然后,此文件中的所有属性都将覆盖src / main / resources / application.properties中的这些属性。
因此,您不需要配置文件和任何其他注释,如@TestPropertySource
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.