@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 都可以成功地从上下文中获取。然而,属性没有被正确加载,因为当我尝试访问它们时,它们全部为空。有什么建议吗?