我想对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);
}
}
Run Code Online (Sandbox Code Playgroud)
和ApplicationTest.java这样
@ComponentScan("org.nerve.jiepu")
@EnableAutoConfiguration()
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我的POM是这样的:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</parent>
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我得到了以下错误信息
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
Run Code Online (Sandbox Code Playgroud)
但是当我将此应用程序作为普通Java应用程序运行时
@SpringBootApplication
public …Run Code Online (Sandbox Code Playgroud) 这是一个Spring bean的片段:
@Component
public class Bean {
@Value("${bean.timeout:60}")
private Integer timeout;
// ...
}
Run Code Online (Sandbox Code Playgroud)
现在我想用JUnit测试来测试这个bean.因此我使用SpringJUnit4ClassRunner和ContextConfiguration注释.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class BeanTest {
@Autowired
private Bean bean;
// tests ...
@Configuration
public static class SpringConfiguration {
@Bean
public Bean bean() {
return new Bean();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,SpringJUnit4ClassRunner无法解析@Value表达式,即使提供了默认值(抛出NumberFormatException).似乎跑步者甚至无法解析表达式.
我的测试中缺少什么?
对于我的一个Spring bean(比如Application类),我使用@Value注释从属性文件(prop.properties)中获取属性的值(my.property.flag = true/false).这完全没问题.
我需要编写一个集成测试(比如说ApplicationIt类),我需要测试属性的值,即true和false.
在我的属性文件中,属性的值设置为true.是否可以从我的Integration测试中将值动态设置为false?
例如,
prop.properties:
my.property.flag=true
Run Code Online (Sandbox Code Playgroud)
应用程序类文件:
@Component
class Application {
//This value is fetched from properties file
//the value is set to true.
@Value(${my.property.flag})
private String isTrue;
......
..........
}
Run Code Online (Sandbox Code Playgroud)
整合测试:
class ApplicationIT {
//how can I set the value of isTrue here to false?
}
Run Code Online (Sandbox Code Playgroud)