Java 在测试环境中实例化context.xml的位置
我们在Java项目中使用SpringFramework(XML版本)4.0.5.RELAESE。 在我们的应用程序中,context.xml在应用程序开始时实例化,并通过依赖注入提供每个属性 现在,我想知道在测试环境中在何处实例化它的最佳(和常用)策略是什么。(我们有一些单元、集成和系统测试,他们至少需要context.xml中提供的DatabaseConnector bean) 我曾想过创建一个抽象类,每个测试都从该类扩展而来,但在本例中,它将在每个测试之前被新实例化。我希望有一个与主应用程序类似的解决方案,在主应用程序中,上下文只实例化一次,其他所有需要的内容都是通过依赖项注入来设置的Java 在测试环境中实例化context.xml的位置,java,spring,unit-testing,testing,Java,Spring,Unit Testing,Testing,我们在Java项目中使用SpringFramework(XML版本)4.0.5.RELAESE。 在我们的应用程序中,context.xml在应用程序开始时实例化,并通过依赖注入提供每个属性 现在,我想知道在测试环境中在何处实例化它的最佳(和常用)策略是什么。(我们有一些单元、集成和系统测试,他们至少需要context.xml中提供的DatabaseConnector bean) 我曾想过创建一个抽象类,每个测试都从该类扩展而来,但在本例中,它将在每个测试之前被新实例化。我希望有一个与主应用程序
研究这个主题没有多大帮助,所以我决定问这个问题。Spring附带了
SpringJUnit4ClassRunner
和@ContextConfiguration
-注释。如果您使用它们,那么spring将在不同的测试中重用相同的spring上下文
因此,Spring测试类可以如下所示:
package com.queomedia;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
//@Transactional
@ContextConfiguration(SpringTestContext.APPLICATION)
public class SpringContextTest {
@Autowire
private ApplicationContext applicationContext;
//Test that the spring context can been loaded
@Test
public void testSpringContextLoad() {
Assert.assertNotNull("application context expected", this.applicationContext);
}
}
spring为内置的junit提供了SpringJUnit4ClassRunner框架。您只需直接在junit本身中配置应用程序上下文。您可以查看更多详细信息您可能指的是“@Autowired”,您知道我为什么不能解析SpringTestContext吗?