繁体   English   中英

"'application.yml' 中的 Spring Boot 属性未从 JUnit 测试加载"

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

我究竟做错了什么? 我正在使用这个运行并找到我的src\/main\/resources\/config\/application.yml<\/code>的小型独立应用程序。 相同的配置不适用于 JUnit,见下文:

@Configuration
@ComponentScan
@EnableConfigurationProperties

public class TestApplication {

    public static void main(String[] args) {

        SpringApplication.run(TestApplication.class);
    }
}


@Component
@ConfigurationProperties

public class Bean{
    ...
}

尝试这个:

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

编辑

对于 Spring Boot 1.5+ 版本,删除了SpringApplicationConfiguration以支持SpringBootTest或直接使用SpringBootContextLoader

您仍然可以将initializers参数与ContextConfiguration注释一起使用。

使用@SpringBootTest在 SpringBoot 2.0 w/o 中加载任何自定义 yml 文件的@SpringBootTest

  • 在 test\\resources 中创建一些 yml 文件
  • 使用ConfigFileApplicationContextInitializerspring.config.location属性

示例代码:

@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.

}

对于 JUnit 5,使用@ExtendWith(SpringExtension.class)注释而不是@RunWith(SpringRunner.class)

这是另一种方式:[Spring Boot v1.4.x]

@Configuration
@ConfigurationProperties(prefix = "own")
public class OwnSettings {

    private String name;
    Getter & setters...

}

import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@BootstrapWith(SpringBootTestContextBootstrapper.class)
public class OwnSettingsTest {

  @Autowired
  private OwnSettings bean;

  @Test
  public void test() {
    bean.getName();
  }
}

这仅在“application.properties”文件存在时才有效。

例如 Maven 项目:

src/main/resources/application.properties [该文件可以为空,但它是强制性的! ]
src/main/resources/application.yml [这是你真正的配置文件]

2017 年 2 月的替代方案:

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

精益变体(没有@SpringBootTest ):

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

使用 Spring Boot 2 进行单元测试

spring boot 2 默认支持 'application.properties',对于 'application.yml' 只需在下面添加:

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

例如

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = { "spring.config.location=classpath:application.yml" })
public class ServiceTest {...}

Spring Boot 2 示例:

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
        .withInitializer(new ConfigFileApplicationContextInitializer());

@Test public void test() throws Exception {

    this.contextRunner
    .withUserConfiguration(TestApplication.class)
    .run((context) -> {

        .....

    });
}

在我的例子中,我试图测试一个没有在常规应用程序类路径中声明@SpringBootApp的库,但我的测试上下文中确实有一个。 在通过 Spring Boot 初始化过程调试我的方式后,我发现 Spring Boot 的YamlPropertySourceLoader (从 1.5.2.RELEASE 开始)不会加载 YAML 属性,除非org.yaml.snakeyaml.Yaml在类路径上。 我的解决方案是在我的 POM 中添加snakeyaml 作为测试依赖项:

    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.19</version>
        <scope>test</scope>
    </dependency>

添加到Liam答案,另一种选择是:

@TestPropertySource(locations = { "classpath:application.yaml" })

这里的主要区别在于,如果application.yaml不在您的/test/resources目录中,则测试将失败并显示文件未找到异常

扩展利亚姆回答

您可以添加spring.config.additional-location=classpath:application-overrides.yaml属性,以便默认位置的配置将与提供的附加配置合并:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = {
  "spring.config.additional-location=classpath:testcases/test-case-properties.yaml",
})
public class SpecificTestCaseIntegrationTest {

由于 spring-boot 版本 2.6.0 org.springframework.boot.test.context.ConfigFileApplicationContextInitializer已被弃用,建议使用org.springframework.boot.test.context.ConfigDataApplicationContextInitializer

在您的测试中,您可以将其用作:

@ContextConfiguration(classes = {
  ...        
}, initializers = ConfigDataApplicationContextInitializer.class)
public class MyTestingClassIT

这有效

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() {
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM