0

在使用 @PostConstruct 创建特定类的多个 bean 之后,根据一个类的多个 bean 实例填充了 application.properties 中的值

我想知道如何测试这些 bean 实例的创建。

我试过这样:

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

    }
}

但这会导致:AppHealthCheckProperties(baseUrl=null, basePath=null, name=null)

4

1 回答 1

1

尝试以下操作:

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

    }
}
于 2021-10-18T23:41:51.157 回答