Home > Software engineering >  How to test multiple instance creation of a bean of one class filled with values from application.pr
How to test multiple instance creation of a bean of one class filled with values from application.pr

Time:10-20

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)

CodePudding user response:

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));

    }
}
  • Related