springboot @ContextConfiguration junit加载配置属性
-
前提
- 不设定resources -> appplication-{}.properties 默认属性
@TestConfiguration public class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(@NonNull ConfigurableApplicationContext applicationContext) { Map<String, String> values = new HashMap<>(); // datasource values.put("spring.datasource.url", "jdbc:postgresql://localhost:5432/{database}"); values.put("spring.datasource.username", "username"); values.put("spring.datasource.password", "p"); TestPropertyValues.of(values).applyTo(applicationContext); }
在unitTest 的类注解: @ContextConfiguration(initializers = Initializer.class)