【发布时间】:2012-03-22 15:22:58
【问题描述】:
我想编写一个 JUnit @Rule(4.10 版)来设置一些对象(一个实体管理器),并通过将它们“注入”到一个变量中来使它们在测试中可用。
类似这样的:
public class MyTestClass() {
@Rule
MyEntityManagerInjectRule = new MyEntityManagerInjectRule():
//MyEntityManagerInjectRule "inject" the entity manager
EntityManger em;
@Test...
}
问题是我不知道如何在 MyEntityManagerInjectRule(扩展 TestRule)中获取 MyTestClass 的当前实例,因为它只有一种方法。
Statement apply(Statement base, Description description);
Description 中只有类 MyTestClass,但没有用于测试的实例。
另一种方法是使用 org.junit.rules.MethodRule 但这已被弃用。 之前和之后不足以完成这项任务,因为那时我需要将代码复制到测试中,而且它们或多或少也被弃用了。 (参见 Block4JClassRunner.withBefores / withAfters)。
所以我的问题是如何在不使用已弃用的东西的情况下访问测试类实例。
【问题讨论】: