Skip to content

Disable @ConfigurationProperties scan in slice tests #16659

@ebussieres

Description

@ebussieres

I've found a regression with version 2.2.0.M2.

I have a dao test which is using @DataJpaTest annotation that looks like this :

@DataJpaTest
@RunWith(SpringRunner.class)
@Sql(executionPhase = BEFORE_TEST_METHOD, scripts = "classpath:beforeTestDaoITest.sql")
@Sql(executionPhase = AFTER_TEST_METHOD, scripts = "classpath:afterTestDaoITest.sql")
public class TestDaoITest {
  @Autowired
  private TestDao underTest;

  @Test
  public void whenFindOne_givenExistingId_thenId() {
    Optional<TestEntity> result = this.underTest.findById(1L);

    assertEquals(result.get().id, (Long) 1L);
  }
}

In the project, I have a configuration class which use @ConfigurationProperties and it looks like this:

@Configuration
@ConfigurationProperties(prefix = "monitoring")
public class Config {
  private Long id;

  @Bean
  public HealthIndicator healthIndicator(HealthAggregator healthAggregator) {
    HealthIndicatorRegistry healthIndicatorRegistry = new DefaultHealthIndicatorRegistry();
    return new CompositeHealthIndicator(healthAggregator, healthIndicatorRegistry);
  }

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }
}

With version 2.1.4.RELEASE, everything works fine. Because it's a DataJpaTest, the Config isn't scanned and the test passes.

With version 2.2.0.M2, the config class is scanned and an error occured in the test because no HealthAggregator bean exists. I think that the reason comes from @ConfigurationProperties, When I remove the @ConfigurationProperties annotation, the config class isn't scanned anymore

I have a working example here that reproduce the problem : https://github.com/ebussieres/spring-boot-2.2-issue

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions