The test profile

For any tests, it is ideal to keep the configuration for them in an isolated environment. The "test" profile is for this purpose. As you have already seen, we use @ActiveProfiles("test") in the unit test code. Instead of looking for src/main/resources/application-test.properties, Spring will look for src/test/resources/application.properties, in which the biggest difference is the datasource configuration. In test "test" profile, we use H2 to speed up the performance. In other environments, we use MySQL.We want to keep H2 only for the tests, and that is why we add the H2 dependency in pom.xml with the test scope.

One interesting thing about the "test" profile is that if you set the active profile to be "test" by using methods other than using the @ActiveProfiles annotation, Spring will look for src/main/resources/application-test.properties. For example, the following are ways to set the active profile to be "test". You will need to have application-test.properties ready if want you do that:

// Set active profile through using the @Profile annotation
@Profile("test")
// Set active profile through Environment.setActiveProfiles()
env.setActiveProfiles("test");
// Set active profile through JVM parameter
$ java -jar -Dspring.profiles.active=test app.jar
# Set active profile through environment variable
export spring_profiles_active=test

The usage of the "test" profile in places other than the unit tests could be confusing because we need to have two .properties for the same profile. Because of this, we will only use the "test" profile in unit tests with the @ActiveProfiles annotation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.38.92