1. Overview
1.概述
Spring brings many features to help us with testing our code. Sometimes we need to use particular configuration properties in order to set up the desired scenario in our test cases.
Spring带来了许多功能来帮助我们测试我们的代码。有时我们需要使用特定的配置属性,以便在我们的测试案例中设置所需的场景。
In these situations, we can make use of the @TestPropertySource annotation. With this tool, we can define configuration sources that have higher precedence than any other source used in the project.
在这些情况下,我们可以利用@TestPropertySource注解。通过这个工具,我们可以定义配置源,其优先级高于项目中使用的任何其他源。
Hence, in this short tutorial, we’ll see examples where we use this annotation. Also, we’ll analyze its default behavior and the main attributes it supports.
因此,在这个简短的教程中,我们将看到我们使用这个注解的例子。同时,我们将分析它的默认行为和它支持的主要属性。
To learn more about testing in Spring Boot, we suggest having a look at our ‘Testing in Spring Boot’ tutorial.
要了解有关Spring Boot测试的更多信息,我们建议看看我们的‘Spring Boot中的测试’教程。
2. Dependencies
2.依赖性
The easiest way to include all the required libraries in our project is by adding the spring-boot-starter-test artifact in our pom.xml file:
在我们的项目中包含所有需要的库的最简单方法是在我们的pom.xml文件中添加spring-boot-starter-test工件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.7.2</version>
</dependency>
We can check Maven Central to verify we’re using the latest version of the starter library.
我们可以检查Maven Central,以验证我们是否使用了最新版本的starter库。
3. How to Use @TestPropertySource
3.如何使用@TestPropertySource?
Let’s imagine we’re using the value of a property by injecting it using the @Value Spring annotation:
让我们设想一下,我们通过使用@Value Spring注解注入一个属性的值。
@Component
public class ClassUsingProperty {
@Value("${baeldung.testpropertysource.one}")
private String propertyOne;
public String retrievePropertyOne() {
return propertyOne;
}
}
We’ll then use the @TestPropertySource class-level annotation to define a new configuration source and override the value of that property:
然后我们将使用@TestPropertySource 类级注解来定义一个新的配置源并覆盖该属性的值。
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ClassUsingProperty.class)
@TestPropertySource
public class DefaultTest {
@Autowired
ClassUsingProperty classUsingProperty;
@Test
public void givenDefaultTPS_whenVariableRetrieved_thenDefaultFileReturned() {
String output = classUsingProperty.retrievePropertyOne();
assertThat(output).isEqualTo("default-value");
}
}
Typically, whenever we use this test annotation we will also include the @ContextConfiguration one so as to load and configure the ApplicationContext for the scenario.
通常情况下,只要我们使用这个测试注解,我们也将包括@ContextConfigurationone,以便为场景加载和配置ApplicationContext。
By default, the @TestPropertySource annotation tries to load a properties file relative to the class that declared the annotation.
默认情况下,@TestPropertySource注解会尝试加载一个相对于声明该注解的类的properties文件。。
In this case, for example, if our test class is in the com.baeldung.testpropertysource package, then we’ll need the file com/baeldung/testpropertysource/DefaultTest.properties in our classpath.
在这种情况下,例如,如果我们的测试类在com.baeldung.testpropertysource包中,那么我们就需要在classpath中找到com/baeldung/testpropertysource/DefaultTest.properties文件。
Let’s add it to our resources folder then:
那么让我们把它添加到我们的资源文件夹中。
# DefaultTest.properties
baeldung.testpropertysource.one=default-value
Additionally, we can change the default configuration file location, or add extra properties that will have even higher precedence:
此外,我们还可以改变默认的配置文件位置,或者添加额外的属性,这些属性将具有更高的优先权:。
@TestPropertySource(locations = "/other-location.properties",
properties = "baeldung.testpropertysource.one=other-property-value")
Finally, we can specify whether we want to inherit locations and properties values from superclasses or not. Hence, we can toggle the inheritLocations and inheritProperties attributes, which are true by default.
最后,我们可以指定我们是否要从超类中继承locations和properties值。因此,我们可以切换inheritLocations和inheritProperties属性,它们默认为true。
4. Conclusion
4.总结
With this simple example, we’ve learned how to use the @TestPropertySource Spring annotation effectively.
通过这个简单的例子,我们已经学会了如何有效地使用@TestPropertySourceSpring注解。
We can find examples for the different scenarios in our Github repository.
我们可以在我们的Github资源库中找到不同情况下的例子。