Skip to content

Inconsistent behaviour of inheritance of @ActiveProfiles #35484

@wimdeblauwe

Description

@wimdeblauwe

I find the inheritance behaviour of @ActiveProfiles to be a bit confusing. According to the documentation the inheritProfiles property allows to define "Whether bean definition profiles from superclasses and enclosing classes should be inherited.".

Technically, this is indeed the behaviour. If I create this meta-annotation:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@SpringBootTest
@ActiveProfiles("integration-test")
public @interface MyAppSpringBootTest {
}

and I have this base class:

@MyAppSpringBootTest
public abstract class AbstractTestClass {
}

Then, I can write this test and it works:

@ActiveProfiles("extra-profile")
public class ExampleTestUsingClass extends AbstractTestClass {

  @Autowired
  private Environment environment;

  @Test
  void test() {
    assertThat(environment.getActiveProfiles())
        .containsExactlyInAnyOrder("integration-test", "extra-profile");
  }
}

What surprised me was that it does not work with an interface. So if I have this interface:

@MyAppSpringBootTest
public interface AbstractTestInterface {
}

Then this test will fail:

@ActiveProfiles("extra-profile")
public class ExampleTestUsingInterface implements AbstractTestInterface {

  @Autowired
  private Environment environment;

  @Test
  void test() {
    assertThat(environment.getActiveProfiles())
        .containsExactlyInAnyOrder("integration-test", "extra-profile");
  }
}

Only extra-profile is active.

What also fails is this:

@MyAppSpringBootTest
@ActiveProfiles("extra-profile")
public class ExampleTest {

  @Autowired
  private Environment environment;

  @Test
  void test() {
    assertThat(environment.getActiveProfiles())
        .containsExactlyInAnyOrder("integration-test", "extra-profile");
  }
}

This last example is what I actually want to use, I like to avoid base classes or interfaces for my test. Just using meta-annotations feels the "cleanest" to me.

Would it be possible to align this behaviour?

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: testIssues in the test modulestatus: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions