3

I am using @Profile Spring annotations to choose between embedded, standalone and container managed data sources. In order to choose 'embedded' my integration tests are annotated to activate the appropriate profile:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes={TestConfigWrapper.class})
@ActiveProfiles({"EMBEDDED_DB"})
public class SomeIntegrationTest {

The problem is that I would like to move '@ActiveProfiles' into TestConfigWrapper, but doing this doesn't get picked up and the application context won't load any DataSources.

This means I have to annotate every integration test with an @ActiveProfile which effectively means it becomes integration test boiler-plate and could easily hamper future refactoring.

Is there a way I can do this using java config?

2
  • @ ContextConfiguration and @ActiveProfiles could be inherited if I'm not mistaken, thus although not flexible but you can place these on an abstract class and make every integration test extend it. Commented Sep 6, 2013 at 1:28
  • Perfect - here's what I ended up using: @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={WebAppInitializer.class}) @ActiveProfiles({Profiles.EMBEDDED_DB}) public abstract class ProfiledIntegrationTest { } Commented Sep 6, 2013 at 8:19

1 Answer 1

4

Per comment from Hippooom use an abstract class to configure tests:

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={WebAppInitializer.class})
@ActiveProfiles({Profiles.EMBEDDED_DB})
public abstract class ProfiledIntegrationTest {

}
Sign up to request clarification or add additional context in comments.

1 Comment

another alternative would've been having this as an annotation. Then you would have just had to use that one annotation on your test classes. Both should have an equal impact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.