spring - 具有 Spring 应用程序上下文的 JUnit 自定义运行器

标签 spring junit integration-testing applicationcontext

我对 Spring 相当陌生,正在为 Web 应用程序使用一套 JUnit 4.7 集成测试。我有许多以下形式的工作测试用例:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/testContext.xml" })

public class myTest {
    @Test
    public void testCreate() {
            //execute tests
            ....
    }
}

我的应用程序有许多正在测试的外部依赖项,所有这些依赖项都有通过加载 testContext.xml 来初始化的 bean。其中一些外部依赖项需要自定义代码来初始化和拆除必要的资源。

我不想在每个需要它的测试类中重复此代码,而是想将其封装到一个公共(public)位置。我的想法是创建一个单独的上下文定义以及一个扩展 SpringJUnit4ClassRunner 并包含 @ContextConfiguration 注释和相关自定义代码的自定义运行器,如下所示:

import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//load the context applicable to this runner
@ContextConfiguration(locations = { "/META-INF/spring/testContext.xml" })

public class MyCustomRunner extends SpringJUnit4ClassRunner {

    public MyCustomRunner(Class<?> clazz) throws InitializationError {
        super(clazz);           
    }

    @Override
    protected Statement withBeforeClasses(Statement statement) {
        // custom initialization code for resources loaded by testContext.xml
                ...

        return super.withBeforeClasses(statement);
    }

    @Override
    protected Statement withAfterClasses(Statement statement) {
        // custom cleanup code for resources loaded by testContext.xml
                ....

        return super.withAfterClasses(statement);
    }

}

然后我可以让每个测试类指定其适用的运行程序:

@RunWith(MyCustomRunner)

当我这样做时,我的测试将运行并执行正确的 withBeforeClasseswithAfterClasses 方法。但是,没有向测试类提供 applicationContext,并且我的所有测试都失败:

java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.

只有当我在每个测试类上指定 @ContextConfiguration 注释时,上下文才会正确加载 - 理想情况下,我希望此注释与它负责加载的资源的处理程序代码一起使用。这引出了我的问题 - 是否可以从自定义运行器类加载 Spring 上下文信息?

最佳答案

可以创建一个测试基类——@ContextConfiguration可以继承,还有@Before@After等:

@ContextConfiguration(locations = { "/META-INF/spring/testContext.xml" }) 
public abstract class myBaseTest { 
    @Before
    public void init() {
        // custom initialization code for resources loaded by testContext.xml 
    }

    @After
    public void cleanup() {
        // custom cleanup code for resources loaded by testContext.xml
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
public class myTest extends myBaseTest { ... }

关于spring - 具有 Spring 应用程序上下文的 JUnit 自定义运行器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5733959/

相关文章:

java - Spring 自动注册模块

Spring RabbitTemplate 未使用 TTL 创建死信队列

java - Spring,在应用程序启动期间复制文件

java - 模拟正在测试的同一类中的 void 方法

integration-testing - 为什么这个简单的 MSpec 测试返回不确定?

java - Spring Data MongoDB 通过内部对象获取文档

java - 避免多个 Not null 语句

java - 没有名为 testPU 的 EntityManager 的持久性提供程序

wcf - 如何以编程方式从 WCF 服务生成 WSDL(集成测试)

c# - 在集成测试中实现完整日志记录