Java 在Spring中使用Mockito模拟属性文件
我正在尝试在控制器中为以下方法编写单元测试Java 在Spring中使用Mockito模拟属性文件,java,spring,properties,mockito,Java,Spring,Properties,Mockito,我正在尝试在控制器中为以下方法编写单元测试 @Autowired private ApplicationContext context; private String getProperty() { try { Properties props = context.getBean("myProperties", Properties.class); String val = props.getProperty("m
@Autowired
private ApplicationContext context;
private String getProperty() {
try {
Properties props = context.getBean("myProperties", Properties.class);
String val = props.getProperty("myProperty");
......
Bean在我的applicationContext中声明如下:
<util:properties id="myProperties" scope="prototype" location="file:${catalina.base}/webapps/myProperties.properties"/>
但是我必须在某个地方将测试文件声明为bean
我想知道有没有更简单的方法
谢谢更简单的方法是使用,而不是直接从spring应用程序上下文中提取属性。PropertyPlaceHolderConfigure使用指定的属性注入bean。然后您根本不需要Mockito,在测试中,您将控制器中的属性值设置为您想要的值 因此,您需要在应用程序上下文xml中设置配置器:
<context:property-placeholder
location="file:${catalina.base}/webapps/myProperties.properties"/>
刚才看到,这是一种新的无xml方式:
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
如果您使用的是spring-3,则可以执行以下操作:
<context:property-placeholder location="myprops.properties" />
这使得myprops.properties可以通过${…}表达式进行变量替换,并且@Value注释允许属性的值注入。然后,在单元测试中,您可以简单地设置myProp的不同值。这样我就可以在不必加载Spring上下文的情况下进行测试。我使用配置类从代码中访问所有属性文件的值。好处是: 1) Spring不会在单元测试中加载 2) 如果属性丢失并且是必需的,则可以强制执行异常 3) 您可以从getter()方法返回强类型属性值(即转换为日期) 4) 属性文件中预期的“键”值记录在单个Java类中(即公共静态最终属性XXX) }
然后,您可以通过简单地注入一个标准java.util.Properties来对这个类进行单元测试,谢谢您的响应。你能用上面的例子告诉我代码是什么样子吗?你在用SpringMVC吗?您可以简单地在控制器上定义一个字段并在控制器的bean定义中为其设置一个值,而不是向控制器提供对ApplicationContext的引用并从中提取值。然后测试就不成问题了,因为您可以在测试中以任何方式初始化控制器-根本不需要模拟应用程序上下文。这与我之前提出的一个问题有关…如果找不到myprops.proeprites怎么办?目前我在try-catch块中有这个属性,因为这个属性文件位于war之外,并且有一点可能它不在spring容器将抛出的正确位置。如果找不到属性,我想您可以尝试应用一些默认值,但我更喜欢容器不启动。如果属性文件丢失或位于错误的位置或其他位置,您是否希望立即查找并修复它,而不是默默地返回到一些可能不正确的属性?
String quux;
public void setQuux(String quux) {this.quux = quux;}
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
<context:property-placeholder location="myprops.properties" />
@Value("${myProperty}")
private String myProp;
public String getMyProp() {
return myProp;
}
@Component
public class Config {
public static final String PROP_USER_NAME = "user.name";
private Properties applicationProperties;
/*** Functional methods ***/
/**
* Helper method to ensure consistent Exception handling where requested property values are missing
* from the properties files and they are "required" for the application to function correctly.
*
* @param key
* @return The String value of the property requested
*/
private String readPropertyRequired(String key) {
String value = readProperty(key);
if(StringUtils.isBlank(value)) {
throw new PropertyNotFoundException(key);
}
return value;
}
/**
* Helper method to return String values from the properties files that have been loaded
*
* @param key
* @return The String value of the property requested or NULL if empty
*/
private String readProperty(String key) {
return applicationProperties.getProperty(key);
}
/*** Getters & Setters ***/
@Autowired
public void setApplicationProperties(Properties applicationProperties) {
this.applicationProperties = applicationProperties;
}
public String getUserName() {
return readPropertyRequired(PROP_USER_NAME);
}