@ActiveProfiles in meta annotation and test class not working

I have created a @EmbeddedMongoDBUnitTest meta annotation that activates two profiles to be used in spring based unit tests. Basic setup works:

@ActiveProfiles({"embeddedMongoDB", "embeddedMongoDBUnitTest"})
public @interface EmbeddedMongoDBUnitTest {

public class WorkingTest {


Now, when trying to activate another profile using a different @ActiveProfiles annotation on the test class itself, the profiles in @EmbeddedMongoDBUnitTest are no longer activated:

public class NotWorkingTest {


Is there any reason this doesn't work or is this a bug in spring test code?


source to share

1 answer

This is not a mistake: this is by design.

The reason this doesn't work is because this form of configuration is simply not supported by Spring.

The algorithm used by the Spring Framework when searching for an annotation stops when it detects the first occurrence of the desired annotation. So in your example, the annotation @ActiveProfiles

is not NotWorkingTest

effectively shadowing the annotation @ActiveProfiles

in your composed annotation @EmbeddedMongoDBUnitTest


Note that these are general annotation semantics in the main Spring framework. In other words, the behavior you are experiencing is not module specific spring-test


Having said that, profiles declared through @ActiveProfiles

are actually inherited in the test class hierarchy (unless you set the flag inheritProfiles

in false

). But don't confuse class hierarchies with annotation hierarchies: Java supports inheritance for interfaces and classes, but not for annotations.

Hope this clears things up!

Sam (component output for module spring-test




All Articles