加载系统属性进行测试
我最近找到了一个解决方案,允许我为单元测试加载系统属性。如果我单独运行测试,它效果很好,但如果我选择运行整个测试套件,它就会失败。有人能告诉我为什么吗?
第一步是加载测试应用程序上下文:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext-test.xml")
下一步是创建一个将加载系统属性的类:
import java.io.InputStream;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.springframework.core.io.Resource;
public class SystemPropertiesLoader{
private Resource resource;
public void setResource(final Resource resource){
this.resource = resource;
}
@PostConstruct
public void applyProperties() throws Exception{
final Properties systemProperties = System.getProperties();
final InputStream inputStream = resource.getInputStream();
try{
systemProperties.load(inputStream);
} finally{
inputStream.close();
}
}
}
最后一步是将其作为 bean 列在我的测试应用程序上下文中:
<bean class="com.foo.SystemPropertiesLoader">
<property name="resource" value="classpath:localdevelopment_Company.properties" />
</bean>
当我运行测试套件时,几个我的所有测试都依赖于系统属性,但都失败了。如果我去具体测试并运行它,它就会通过。我已经对其进行了调试,并且验证了 SystemPropertiesLoader 中的代码正在执行,并且所有其他 bean 都已从上下文中成功提取。但是,这些属性未正确加载,因为当我尝试访问它们时,它们都为空。有什么建议吗?
I recently found a solution that allows me to load system properties for my unit tests. It works great if I'm running a test individually, but if I choose to run the whole test suite, it fails. Can someone tell me why?
The first step is to load the test application context:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext-test.xml")
The next step is to create a class which will load the system properties:
import java.io.InputStream;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.springframework.core.io.Resource;
public class SystemPropertiesLoader{
private Resource resource;
public void setResource(final Resource resource){
this.resource = resource;
}
@PostConstruct
public void applyProperties() throws Exception{
final Properties systemProperties = System.getProperties();
final InputStream inputStream = resource.getInputStream();
try{
systemProperties.load(inputStream);
} finally{
inputStream.close();
}
}
}
The final step is to list this as a bean in my test application context:
<bean class="com.foo.SystemPropertiesLoader">
<property name="resource" value="classpath:localdevelopment_Company.properties" />
</bean>
When I run the test suite, several of my tests, all of which rely on system properties, fail. If I go to the specific test and run it, it will pass. I've debugged it and I've verified that the code in SystemPropertiesLoader is being executed, and all other beans are being pulled successfully from the context. However, the properties are not being loaded correctly, as they are all coming up null when I try to access them. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些想法:
A few ideas:
问题实际上是 Properties 类中的值是静态定义的。因此,这里是破坏解决方案的情况:
最后,最好的解决方案是在 Properties 类中定义默认值。
The problem was actually that the values from the Properties class were defined statically. So here's the case that broke the solution:
In the end, the best solution was to define default values within the Properties class.
您的每个测试用例是否有可能生成一个新的 JVM,并且没有为每个测试用例设置
System
属性?也许尝试在 JUnit 测试类中利用
setUp()
和tearDown()
方法。Could it be possible that each of your test cases is spawning a new JVM, and the
System
properties are not being set for each test case?Maybe try to leverage the
setUp()
andtearDown()
methods in your JUnit test class.