Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying multiple Spring profiles to a single Configuration class

I have 4 possible Spring profiles for my application

  • TEST (This is for Unit/Integration testing)
  • QA
  • STAGE
  • PRODUCTION

The last 3 in the above list are actutal environments in which this application will run.

Here's the overview of my configuration class called AppConfig

@Configuration
@ComponentScan(basePackages={"org.mycompany.services","org.mycompany.domain", "org.mycompany.utils"})
public class AppContext {

}

Here's my question: I want the AppContext's configuration to be applied for QA, STAGE and PRODUCTION profiles only (But not for TEST profile). What's the best way to do that?

The reason for this is that the above config class scans & picks classes from org.mycompany.services. But I do not want this package to be picked up for TEST profile as I'm intending to mock the classes in this package using Mockito.

Another possible question is if I need to rethink the way I'm approaching Unit/Integration testing so that I can simplify mocking of classes in org.mycompany.service package?

like image 515
user6123723 Avatar asked Feb 15 '26 01:02

user6123723


1 Answers

You can use @Profile annotation on the configuration class.

To enable the class for any profile except TEST, just use it like @Profile("!TEST").

Or you can explicitly specify the profiles like so: @Profile({ "QA", "STAGE", "PRODUCTION" })

like image 186
Bohuslav Burghardt Avatar answered Feb 16 '26 13:02

Bohuslav Burghardt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!