如何测试用 application.properties 中的值填充一个 class 的 bean 的多实例创建?
How to test multiple instance creation of a bean of one class filled with values from application.properties?
根据
使用@PostConstruct 创建特定class 的多个bean 之后
我想知道如何测试这些 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)
尝试以下操作:
@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));
}
}
根据
我想知道如何测试这些 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)
尝试以下操作:
@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));
}
}