在测试开始时,Spring 开始创建上下文。我的根配置测试 XML 文件开始是:
<!-- <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:trx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:util="http://www.springframework.org/schema/util"
xmlns:ctx="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.4.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
">
<bean id="mailProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:beans_sets/test.properties" />
</bean>
<bean id="U01UhradaService" class="amcssz.spr.srv.main.service.U01UhradaService" />
<import resource="classpath:META-INF/spring/spr-properties.xml" />
</beans>
spr-properties 文件是真实文件,用于部署真实应用程序。它从命令行参数中获取一些变量。其中有 spr.root.dir 变量。在测试中,我必须将其设置在其他地方。我使用了来自https://stackoverflow.com/a/36094573/715269的建议。在根配置测试 XML 文件中,我定义了设置属性的文件,并设置
spr.root.dir="apv/main-app/work"
另外,我试过
spr.root.dir=apv/main-app/work
但是,我仍然收到“无法加载 ApplicationContext”
Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties;
nested exception is java.io.FileNotFoundException: ${spr.root.dir}\spr.properties (The system cannot find the path specified)
测试类:
package amcssz.spr.srv.main.jobs;
import amcssz.spr.srv.main.service.U01UhradaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
@ContextConfiguration(locations={ "classpath:beans_sets/UhradyForIns.xml" })
public class OdeslatADostatJakobyAutomatickyUhradu extends AbstractTestNGSpringContextTests {
@Autowired( required = true )
U01UhradaService service;
...
}
看来我不明白如何设置 Spring 配置变量。