0

After creating multiple beans of a specific class using @PostConstruct according to Multiple instances of a bean of one class filled with values from application.properties

I wonder how I could test the creation of those bean instances.

I tried it this way:

@ActiveProfiles("test")
@RequiredArgsConstructor
@ExtendWith({SpringExtension.class, MockitoExtension.class})
@SpringBootTest(classes = {AppHealthCheckContributor.class, AppHealthCheckProperties.class})
@ConfigurationProperties(prefix = "healthcheck")
@ContextConfiguration(classes = { AppHealthCheckConfig.class })
//@TestPropertySource(properties = "healthcheck.app[0].name=testHealthIndicator, healthcheck.app[0].baseUrl=http://localhost, healthcheck.app[0].basePath=/test")
@TestPropertySource(locations = {"/application-test.properties"})
class AppHealthCheckConfigTest {

    @Autowired
    private AdapterHealthCheckConfig config;

    @Test
    void healthCheckBeansAreInitialized() {
//        config.init();
        System.out.println(config.getApps().get(0));

    }
}

but this results in: AppHealthCheckProperties(baseUrl=null, basePath=null, name=null)

Jonas
  • 121,568
  • 97
  • 310
  • 388
du-it
  • 2,561
  • 8
  • 42
  • 80

1 Answers1

1

Try the following:

@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = AdapterHealthCheckConfig.class)
@TestPropertySource("classpath:application-test.properties")
class AppHealthCheckConfigTest {

    @Autowired
    private AdapterHealthCheckConfig config;

    @Test
    void healthCheckBeansAreInitialized() {
//        config.init();
        System.out.println(config.getApps().get(0));

    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
  • The first version works well. The second doesn't. This produces a java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 – du-it Oct 19 '21 at 17:03
  • Thanks for the feedback. I will remove the second option ;) – João Dias Oct 19 '21 at 17:10