FrV*_*aBe 174 java unit-testing spring-boot
我有一个Spring-Boot应用程序,其中默认属性设置在application.properties类路径中的文件中(src/main/resources/application.properties).
我想在我的JUnit测试中覆盖一些默认设置,其中包含在test.properties文件中声明的属性(src/test/resources/test.properties)
我通常会为我的Junit测试提供专用的Config类,例如
package foo.bar.test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {
}
Run Code Online (Sandbox Code Playgroud)
我首先想到@PropertySource("classpath:test.properties")在TestConfig类中使用可以解决这个问题,但这些属性不会覆盖application.properties设置(请参阅Spring-Boot参考文档 - 23.外部化配置).
然后我尝试-Dspring.config.location=classpath:test.properties在调用测试时使用.这很成功 - 但我不想为每次测试执行设置此系统属性.因此我把它放在代码中
@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {
static {
System.setProperty("spring.config.location", "classpath:test.properties");
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,再次没有成功.
必须有一个关于如何application.properties在JUnit测试中覆盖设置的简单解决方案test.properties,我必须忽略它.
And*_*son 267
您可以使用@TestPropertySource覆盖值application.properties.从它的javadoc:
测试属性源可用于有选择地覆盖系统和应用程序属性源中定义的属性
例如:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*nch 62
您还可以使用元注释来外部化配置.例如:
@RunWith(SpringJUnit4ClassRunner.class)
@DefaultTestAnnotations
public class ExampleApplicationTests {
...
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public @interface DefaultTestAnnotations { }
Run Code Online (Sandbox Code Playgroud)
Moh*_*ish 58
src/test/resources/application.properties如果使用以下注释,Spring Boot会自动加载
@RunWith(SpringRunner.class)
@SpringBootTest
Run Code Online (Sandbox Code Playgroud)
因此,重命名test.properties为application.properties使用自动配置.
如果**只需要加载属性文件(进入环境),您也可以使用下面的,因为解释这里
Run Code Online (Sandbox Code Playgroud)@RunWith(SpringRunner.class) @ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
[ 更新:覆盖某些测试属性 ]
src/main/resources/application-test.properties.@ActiveProfiles("test").此负载application.properties和然后 application-test.properties属性到用于测试案例应用背景下,作为每定义的规则在这里.
演示 - https://github.com/mohnish82/so-spring-boot-testprops
Nim*_*ari 12
如果使用@SpringBootTest注释,则另一种适合覆盖测试中的一些属性的方法:
@SpringBootTest(properties = {"propA=valueA", "propB=valueB"})
Run Code Online (Sandbox Code Playgroud)
jum*_*key 11
如果你像我和你有相同application.properties的src/main/resources和src/test/resources,和你想知道为什么application.properties在你的测试文件夹中未覆盖的application.properties在你的主要资源,阅读...
如果你有application.properties下src/main/resources和相同application.properties下src/test/resources,其application.properties被捞起,取决于你如何运行测试。文件夹结构 src/main/resources和src/test/resources, 是 Maven 架构约定,因此如果您像mvnw test甚至一样运行测试gradlew test,application.propertiesinsrc/test/resources将被选中,因为测试类路径将在主类路径之前。但是,如果您像Run as JUnit Test在 Eclipse/STS 中一样运行测试,则application.propertiesinsrc/main/resources将被选中,因为主类路径在测试类路径之前。
您可以通过打开菜单栏 来查看Run > Run Configurations > JUnit > *your_run_configuration* > Click on "Show Command Line"。
你会看到这样的事情:
XXXbin\javaw.exe -ea -Dfile.encoding=UTF-8 -classpath
XXX\workspace-spring-tool-suite-4-4.5.1.RELEASE\project_name\bin\main;
XXX\workspace-spring-tool-suite-4-4.5.1.RELEASE\project_name\bin\test;
您是否看到classpath xxx\main首先出现,然后是xxx\test?是的,这都是关于类路径的:-)
旁注:请注意,在启动配置中覆盖的属性(例如在 Spring Tool Suite IDE 中)优先于 application.properties。
现在,一切都可以在 Spring 中进行配置。您可以更改构建类路径,以便xxx\test首先出现,然后是xxx\main。
只需转到Project > Properties > Java Build Path > Order and Export,通过将任何测试文件夹放在第一位来更改构建类路径顺序,例如:
就是这样!
但是,在测试时,更好的解决方案是激活src/test/resources/application-{profile}.properties(profile可以测试的位置),例如以下内容src/main/resources/application.properties:
spring.profiles.active=测试
这更简洁,让您可以完全控制在做什么时要激活的配置文件。
Fel*_*ati 11
如果您使用 Spring 5.2.5 和 Spring Boot 2.2.6 并且只想覆盖几个属性而不是整个文件。您可以使用新的注释:@DynamicPropertySource
@SpringBootTest
@Testcontainers
class ExampleIntegrationTests {
@Container
static Neo4jContainer<?> neo4j = new Neo4jContainer<>();
@DynamicPropertySource
static void neo4jProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我要做的是拥有标准src/main/resources/application.properties,并且在src/test/resources/application-default.properties其中我覆盖了所有测试的某些设置。
我遇到了同样的问题,到目前为止也没有使用配置文件。现在必须这样做并记得声明配置文件似乎很麻烦-这很容易被忘记。
诀窍是,利用特定application-<profile>.properties于配置文件的配置来覆盖常规配置文件中的设置。参见https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties。
| 归档时间: |
|
| 查看次数: |
184794 次 |
| 最近记录: |