What is the difference between using @Profile and @ActiveProfiles on a Spring Test configuration
@Configuration @EnableRetry @ActiveProfiles("unittest") static class ContextConfiguration {
and
@Configuration @EnableRetry @Profile("unittest") static class ContextConfiguration {
Profiling is the process of examining an application to locate memory or performance-related issues. When profiling a Java application, you can monitor the Java Virtual Machine (JVM) and obtain data about application performance, including method timing, object allocation and garbage collection.
The default profile is always active. Spring Boot loads all properties in application. yml into the default profile. We could rename the configuration file to application-default.
The @Profile annotation may be used in any of the following ways: as a type-level annotation on any class directly or indirectly annotated with @Component , including @Configuration classes. as a meta-annotation, for the purpose of composing custom stereotype annotations. as a method-level annotation on any @Bean ...
ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes. Meaning it is only supposed to be used to declare active Spring profiles for test classes.
@ActiveProfiles are used in Spring integration test as following. To Activate multiple profiles using @ActiveProfiles, specify profiles as an array. Here on this page we will create Spring integration test classes using @ActiveProfiles annotation with complete example.
I have written a very simple example to demonstrate the power of @Profile feature in Spring 4. This example declares data source for dev and prod. By using the @Profile annotation, a suitable data source will be invoked by the test class. The syntax for this annotation is @Profile (“dev”).
Get Active Profiles Spring's active profiles drive the behavior of the @Profile annotation for enabling/disabling beans. However, we may also wish to access the list of active profiles programmatically. We have two ways to do it, using Environment or spring.active.profile. 6.1. Using Environment
Spring Profiles provide a way to segregate parts of your application configuration.
Any @Component
or @Configuration
can be marked with @Profile
to limit when it is loaded which means that component or configuration will be loaded in the application context only when the active profiles is same as the profile mapped to a component.
To mark a profile active, spring.profiles.active
property must be set in application.properties
or given as an VM argument as -Dspring.profiles.active=dev
While writing Junit, you would want to activate some profile so as to load the required configuration or Component. Same can be achieved by using @ActiveProfile
annotation.
Consider a configuration class which is mapped to profile dev
@Configuration @Profile("dev") public class DataSourceConfig { @Bean public DataSource dataSource() { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost/test"); ds.setUsername("root"); ds.setPassword("mnrpass"); return ds; } @Bean public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); } }
Consider a configuration class which is mapped to profile prod
@Configuration @Profile("prod") public class DataSourceConfig { @Bean public DataSource dataSource() { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:oracle://xxx.xxx.xx.xxx/prod"); ds.setUsername("dbuser"); ds.setPassword("prodPass123"); return ds; } @Bean public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); } }
So, if you want to run your junit test cases in dev
profile then you have to use the @ActiveProfile('dev')
annotation. This will load the DataSourceConfig bean defined in dev profile.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @ActiveProfiles("dev") public class Tests{ // Junit Test cases will use the 'dev' profile DataSource Configuration }
Conclusion
@Profile
is used to map a class to a profile
@ActiveProfile
is used to activate a particular profile(s) during junit test class execution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With