我对 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)
当我这样做时,我的测试将运行并执行正确的 withBeforeClasses 和 withAfterClasses 方法。但是,没有向测试类提供 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/