聚合具有相互依赖关系的 JUnit 规则

gel*_*d0r 7 java junit unit-testing junit-rule

在更复杂的单元测试中,我经常需要存在一组特定的规则。其中一些规则与另一个规则有依赖关系。由于排序是相关的,因此我使用 RuleChains。到目前为止一切都很好。

然而,这在大多数测试中都是重复的(偶尔会使用附加规则)。这种重复不仅感觉没有必要,重复起来也很麻烦,而且很多地方都需要调整,什么时候应该集成一个额外的Rule。

我想要的是规则规则,即包含或聚合其他(特定于应用程序和测试的)规则的(预定义)规则。

我将举例说明目前的情况:

public LoggingRule logRule = new LogRule();
public ConfigurationRule configurationRule = new ConfigurationRule();
public DatabaseConnectionRule dbRule = new DatabaseConnectionRule();
public ApplicationSpecificRule appRule = new ApplicationSpecificRule();

@Rule
RuleChain chain = RuleChain.outerRule(logRule)
                           .around(configurationRule)
                           .around(dbRule)
                           .around(appRule);
Run Code Online (Sandbox Code Playgroud)

假设给定的规则相互依赖,例如 ApplicationSpecificRule 要求首先执行 DatabaseConnectionRule 以建立连接,ConfigurationRule 已经初始化了一个空配置等。还假设对于这个(相当复杂的测试)所有规则都是实际需要。

到目前为止,我能想到的唯一解决方案是创建返回预定义 RuleChain 的工厂方法:

public class ApplicationSpecificRule extends ExternalResource
{
    public static RuleChain basicSet()
    {
         return RuleChain.outerRule(new LogRule())
                         .around(new ConfigurationRule())
                         .around(new DatabaseConnectionRule())
                         .around(new ApplicationSpecificRule());
    }
}
Run Code Online (Sandbox Code Playgroud)

在测试中,这可以按如下方式使用:

@Rule
RuleChain chain = ApplicationSpecificRule.basicSet();
Run Code Online (Sandbox Code Playgroud)

这样就消除了重复,并且可以轻松集成其他规则。甚至可以向该规则链添加特定于测试的规则。但是,当需要进行额外设置时,您无法访问包含的规则(假设您需要ApplicationSpecificRule为了创建某些域对象等)。

理想情况下,这将扩展为还支持使用其他预定义的集合,例如advandancedSet构建在basicSet规则之上的集合。

这可以以某种方式简化吗?首先这是一个好主意还是我以某种方式滥用了规则?对重组测试有帮助吗?想法?

Nam*_*ter 6

TestRule接口只有一个方法,因此可以很容易地定义您自己的自定义规则,该规则委托给 aRuleChain并保留对其他规则的引用:

public class BasicRuleChain implements TestRule {
  private final RuleChain delegate;
  private final DatabaseConnectionRule databaseConnectionRule
      = new DatabaseConnectionRule();

  public BasicRuleChain() {
    delegate = RuleChain.outerRule(new LogRule())
        .around(new ConfigurationRule())
        .around(databaseConnectionRule)
        .around(new ApplicationSpecificRule());
  }

  @Override
  public Statement apply(Statement base, Description description) {
    return delegate.apply(base, description
  }

  public Connection getConnection() {
    return databaseConnectionRule.getConnection();
  }
}
Run Code Online (Sandbox Code Playgroud)