Spring Boot properties in 'application.yml' not loading from JUnit Test

前端 未结 10 507
野趣味
野趣味 2021-01-31 13:59

What am I doing wrong? I\'m using this little standalone App which runs and finds my src/main/resources/config/application.yml. The same configuration doesn\'t work

相关标签:
10条回答
  • 2021-01-31 14:33

    Spring boot 2 example:

    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
            .withInitializer(new ConfigFileApplicationContextInitializer());
    
    @Test public void test() throws Exception {
    
        this.contextRunner
        .withUserConfiguration(TestApplication.class)
        .run((context) -> {
    
            .....
    
        });
    }
    
    0 讨论(0)
  • 2021-01-31 14:38

    Try this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = TestApplication.class, 
        initializers = ConfigFileApplicationContextInitializer.class)
    public class SomeTestClass {
        ...
    }
    

    EDIT:

    For Spring Boot version 1.5+, SpringApplicationConfiguration was removed in favour of SpringBootTest or direct use of SpringBootContextLoader.

    You can still use initializers parameter with ContextConfiguration annotation.

    0 讨论(0)
  • 2021-01-31 14:38

    Alternative in February 2017:

    @SpringBootTest
    @ContextConfiguration(classes = { TestApplication.class })
    @RunWith(SpringRunner.class)
    public class SomeTestClass {
       ...
    }
    

    the lean variant (withouth @SpringBootTest):

    @ContextConfiguration(classes = { TestApplication.class },
                     initializers = { ConfigFileApplicationContextInitializer.class })
    @RunWith(SpringRunner.class)
    public class SomeTestClass {
    
    0 讨论(0)
  • 2021-01-31 14:40

    The trick to load any custom yml file in SpringBoot 2.0 w/o using @SpringBootTest

    • create some yml file in test\resources
    • Use ConfigFileApplicationContextInitializer and spring.config.location property

    Example Code:

    @RunWith(SpringRunner.class)
    @ContextConfiguration(
        classes = { MyConfiguration.class, AnotherDependancy.class },
        initializers = {ConfigFileApplicationContextInitializer.class} )
    @TestPropertySource(properties = { "spring.config.location=classpath:myApp-test.yml" })
    public class ConfigProviderTest {
        @Autowired
        private MyConfiguration myConfiguration; //this will be filled with myApp-test.yml 
    
       @Value("${my.config-yml-string}")
       private String someSrting; //will get value from the yml file.
    
    }
    

    For JUnit 5 use the @ExtendWith(SpringExtension.class) annotation instead of @RunWith(SpringRunner.class)

    0 讨论(0)
  • 2021-01-31 14:45

    Unit test with Spring Boot 2

    spring boot 2 support 'application.properties' by default, for 'application.yml' just add below:

    @TestPropertySource(properties = { "spring.config.location=classpath:application.yml" })
    

    e.g.

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @TestPropertySource(properties = { "spring.config.location=classpath:application.yml" })
    public class ServiceTest {...}
    
    0 讨论(0)
  • 2021-01-31 14:45

    This works

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTest {
    
        @Test
        public void contextLoads() {
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题