我做错了什么?我正在使用这个小的独立应用程序运行并找到我的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{
...
}
以下操作不起作用,application.yml
中的相同属性未加载且Bean
仅包含null
个值:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestApplication.class)
public class SomeTestClass {
...
}
答案 0 :(得分:35)
试试这个:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class,
initializers = ConfigFileApplicationContextInitializer.class)
public class SomeTestClass {
...
}
修改强>:
对于Spring Boot版本1.5+,SpringApplicationConfiguration
被删除,转而使用SpringBootTest
或直接使用SpringBootContextLoader
。
您仍然可以将initializers
参数与ContextConfiguration
注释一起使用。
答案 1 :(得分:8)
这是另一种方式:[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();
}
}
这仅适用于“app.properties'文件存在。
<强> e.g。 maven项目:
src / main / resources / application.properties [该文件可以为空,但必须是!]
src / main / resources / application.yml [这里是你的真实配置文件]
答案 2 :(得分:8)
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 {
答案 3 :(得分:7)
使用@SpringBootTest
ConfigFileApplicationContextInitializer
和spring.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.
}
答案 4 :(得分:2)
使用Spring Boot 2进行单元测试
spring boot 2默认支持'application.properties', 对于“ application.yml”,只需在下面添加:
Item was deleted.
Threat name:
Trojan.Agent.DQGP.
C:\Users\Me\AppData\Roaming\npm\node_modules\nodemon\node_modules\flatmap-stream\index.min.js
例如
@TestPropertySource(properties = { "spring.config.location=classpath:application.yml" })
答案 5 :(得分:1)
在我的情况下,我试图测试一个没有在常规应用程序类路径中声明的@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>
答案 6 :(得分:0)
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) -> {
.....
});
}
答案 7 :(得分:0)
@TestPropertySource(locations = { "classpath:application.yaml" })
此处的主要区别在于,如果application.yaml
不在您的/test/resources
目录中,则测试将失败,并且找不到文件异常
答案 8 :(得分:0)
您可以添加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 {
答案 9 :(得分:-3)
这有效
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() {
}
}