我想对 Spring-boot 进行 Junit 测试,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationTest.class})
public class TestOnSpring {
@Value("${app.name}")
private String appName;
@Test
public void testValue(){
System.out.println(appName);
}
}
和ApplicationTest.java像这样
@ComponentScan("org.nerve.jiepu")
@EnableAutoConfiguration()
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class, args);
}
}
我的 POM 是这样的:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</parent>
当我运行测试时,我得到以下错误信息
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.name' in string value "${app.name}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:807)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
... 31 more
但是当我像普通 Java 应用程序一样运行这个应用程序时
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
效果很好!
有什么问题吗?我应该如何使用Spring-boot进行junit测试? 非常感谢!
您需要添加
@PropertySource(“类路径:application.properties”)
您的班级,因此它会选择您的正常配置。
如果您需要不同的配置进行测试,您可以添加
@TestPropertySource(位置=“classpath:test.properties”)
如果不只是将配置文件复制粘贴到
test/resources
文件夹,那么启动将从那里选择。
参见这个。
您可以使用
@SpringBootTest
来自动创建 PropertySourcesPlaceholderConfigurer
。
Spring Boot 文档的测试章节对此进行了描述。
您已用
@ContextConfiguration(classes = {ApplicationTest.class})
注释了您的测试类。其中 ApplicationTest.class
执行组件扫描所提到的包。当您运行测试时,它会尝试从“main”而不是“test”中的资源文件夹中查找配置。如果您在这种特殊情况下用 @SpringBootTest(classes = {ClassToBeTested.class})
或仅 @SpringBootTest
注释您的类,我认为(不是 100% 确定)它将创建一个有限的上下文并从测试/资源中获取属性。
如果您的属性是特定于测试的,您可以将属性/yml 文件命名为
application-test.properties
或 application-test.yml
。并在测试类中使用 @ActiveProfiles("test")
,以便它始终读取测试特定的属性文件。
我通常使用这个适合我的解决方案。
对我来说,问题与应用程序属性的配置文件集有关。
application.yml
:spring:
config:
activate:
on-profile: local
mongodb:
uri: mongodb+srv:/
@Value("${mongodb.uri}")
private String uri;
@SpringBootTest
@ActiveProfiles("local")
class myTestClass {
我正在使用
@ActiveProfiles("test")
我的测试类中的注释,因此我将所需的值添加到
application-test.yml
文件中并且它起作用了。