Java “application.yml”中的 Spring Boot 属性未从 JUnit 测试加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33213854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot properties in 'application.yml' not loading from JUnit Test
提问by aliopi
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 from JUnit, see below:
我究竟做错了什么?我正在使用这个独立的小应用程序,它运行并找到我的src/main/resources/config/application.yml
. 相同的配置在 JUnit 中不起作用,见下文:
@Configuration
@ComponentScan
@EnableConfigurationProperties
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class);
}
}
@Component
@ConfigurationProperties
public class Bean{
...
}
The following doesn't work, the same properties in application.yml
are not loaded and Bean
has only null
values:
以下不起作用,application.yml
未加载相同的属性并且Bean
只有null
值:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestApplication.class)
public class SomeTestClass {
...
}
采纳答案by luboskrnac
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
.
对于 Spring Boot 1.5+ 版,SpringApplicationConfiguration
已删除以支持SpringBootTest
或直接使用SpringBootContextLoader
.
You can still use initializers
parameter with ContextConfiguration
annotation.
您仍然可以使用initializers
带ContextConfiguration
注释的参数。
回答by Juha Hanka
Here's another way: [Spring Boot v1.4.x]
这是另一种方式:[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();
}
}
This works ONLY if also 'application.properties' file exists.
这仅在“application.properties”文件存在时才有效。
e.g. maven project:
例如 Maven 项目:
src/main/resources/application.properties [ The file can be empty but it's mandatory!]
src/main/resources/application.yml [here's your real config file]
src/main/resources/application.properties [该文件可以为空,但它是强制性的!]
src/main/resources/application.yml [这是你真正的配置文件]
回答by aliopi
Alternative in February 2017:
2017 年 2 月的替代方案:
@SpringBootTest
@ContextConfiguration(classes = { TestApplication.class })
@RunWith(SpringRunner.class)
public class SomeTestClass {
...
}
the lean variant (withouth @SpringBootTest
):
精益变体(没有@SpringBootTest
):
@ContextConfiguration(classes = { TestApplication.class },
initializers = { ConfigFileApplicationContextInitializer.class })
@RunWith(SpringRunner.class)
public class SomeTestClass {
回答by Cesar Gomez Velazquez
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() {
}
}
回答by Lyle
In my case I was trying to test a library without a @SpringBootApp
declared in the regular app classpath, but I do have one in my test context. After debugging my way through the Spring Boot initialization process, I discovered that Spring Boot's YamlPropertySourceLoader(as of 1.5.2.RELEASE) will not load YAML properties unless org.yaml.snakeyaml.Yaml
is on the classpath. The solution for me was to add snakeyaml as a test dependency in my POM:
在我的例子中,我试图测试一个没有@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>
回答by Ritesh
Spring boot 2 example:
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) -> {
.....
});
}
回答by Yarix
The trick to load anycustom yml file in SpringBoot 2.0 w/o using @SpringBootTest
在 SpringBoot 2.0 w/o 中加载任何自定义 yml 文件的技巧@SpringBootTest
- create some yml file in test\resources
- Use
ConfigFileApplicationContextInitializer
andspring.config.location
property
- 在 test\resources 中创建一些 yml 文件
- 使用
ConfigFileApplicationContextInitializer
和spring.config.location
财产
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)
对于 JUnit 5,使用@ExtendWith(SpringExtension.class)
注释而不是@RunWith(SpringRunner.class)
回答by Liam
Unit test with Spring Boot 2
使用 Spring Boot 2 进行单元测试
spring boot 2 support 'application.properties' by default, for 'application.yml' just add below:
spring boot 2 默认支持 'application.properties',对于 'application.yml' 只需在下面添加:
@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 {...}
回答by Dexter Legaspi
adding to Liam's answer, an alternative will be:
@TestPropertySource(locations = { "classpath:application.yaml" })
the key difference here is that the test will fail with a file not found exception if application.yaml
is not in your /test/resources
directory
这里的主要区别在于,如果application.yaml
不在您的/test/resources
目录中,则测试将失败并显示文件未找到异常