springboot @ContextConfiguration junit加载配置属性

  • 前提

    1. 不设定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)

posted @ 2022-05-16 15:00  cc-31415926  阅读(364)  评论(0编辑  收藏  举报