2

We have a set of integration tests that we use to verify various things, including that our database is in a sane state per environment (Dev,Test,Qa,etc) in correlation to our JPA mappings and other data dependent items.

We configure which database, and other environment specific things we are connecting to via a Spring Profile. This works great on our automated build, where we set the environment variable "spring.profiles.active" to whatever environment we are in, run the integration tests, and easily locate configuration errors, and other things that can sometimes be overlooked.

However, what I would like, is the ability to set the spring.profiles.default attribute to "dev" in the integration test suite so that when a developer runs them from the IDE, they don't get errors complaining about the lack of a profile being set. We do this in the web.xml for a real deployment, but what I want to know is how can I hook into the AbstractTransactionalJUnit4SpringContextTests (or elsewhere) to accomplish this?

Matt Broekhuis
  • 1,985
  • 2
  • 26
  • 35

2 Answers2

0

Out of top of my head, you're using AbstractTransactionalJUnit4SpringContextTests which means that you can have access to an instance of ConfigurableApplicationContext. I believe you can say:

class BaseTest extends ATJ4SCT {

  @Autowired
  ConfigurableApplicationContext context;

  @Before
  public void setUp() {
    ConfigurableEnvironment env = context.getEnvironment();
    env.setDefaultProfiles("dev");
    // not sure but if necessary
    context.refresh();

  }

}

Reference: ConfigurableEnvironment#setDefaultProfiles(String...).

Although different topic but may be also worth it to take a look at this question.

Community
  • 1
  • 1
nobeh
  • 9,784
  • 10
  • 49
  • 66
  • yeah.... i thought about \\@before, but then that has to be in every test, or I have to introduce another layer, add the \\@before there, and extend from that (I believe \\@Before executes as you'd expect in an inherited test). What would be "nice" is if AbstractTransactionalJUnit4SpringContextTests had some sort of hook to set up stuff before it initializes the context. A sort of "hey, here's your chance to pretend you're in a servlet container" setup. – Matt Broekhuis Oct 10 '13 at 15:44
0

does not seem to be a way of doing this, so resorted to setting the environment variable spring.profiles.active to "dev" in the default run config for junit in Intellij.

Matt Broekhuis
  • 1,985
  • 2
  • 26
  • 35