Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在测试环境中实例化context.xml的位置_Java_Spring_Unit Testing_Testing - Fatal编程技术网

Java 在测试环境中实例化context.xml的位置

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) 我曾想过创建一个抽象类,每个测试都从该类扩展而来,但在本例中,它将在每个测试之前被新实例化。我希望有一个与主应用程序

我们在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吗?