Groovy类中的@Rule(JUnit)声明和赋值的逻辑是什么? [英] What is the logic of @Rule(JUnit) declaration and assignment in a Groovy class

查看:129
本文介绍了Groovy类中的@Rule(JUnit)声明和赋值的逻辑是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个groovy文件中尝试创建规则创建的一些变体,我认识到@Rule没有描述DECLARATION,而是描述了ASSIGNMENT。
因此,跑步者在加载测试时,会尝试每个规则以进行正确的分配。

  //正确的变体:
@Rule
ErrorCollector collector1 = new ErrorCollector();

public ErrorCollector collector2 = null;
@Rule
collector2 = new ErrorCollector();

public ErrorCollector collector3;
@Rule
collector3 = new ErrorCollector();


//不正确的变体:
@Rule
ErrorCollector collector4 = null;

@Rule
public ErrorCollector collector5;

@Rule
public ErrorCollector collector5 = somethingThatIsNotRule;

@Rule
public ErrorCollector collector5 = anotherRule;

但是,我遇到了一些矛盾的变体:

  //这些行不仅可以被runner使用,而且可以正确传递:
public ErrorCollector collector6;
{
@规则
collector6 = null;
}

public ErrorCollector collector7 = null;
{
@规则
collector7 = null;
}

它的逻辑是什么?



这似乎是Runner中的一个bug - 跑步者在构造测试之前进行过度检查。

在Java中,JUnit运行器检查 @Rule 注释是否应用于公共非静态字段或公共非静态方法,该方法返回 TestRule MethodRule



如果在字段或方法上存在 @Rule 注释,那么该值必须是非空值,否则在测试执行期间得到一个NullPointerException。



您的示例比这更复杂,因为Groovy是一种动态语言,所以它在运行时进行检查,而不是编译 时间。我怀疑collector2和collector3实际上并没有做任何事情。 @Rule 注释不适用于该字段。

  collector4 => NullPointerException 
collector5 =>与收藏家5相同
collector5a =>当你执行时,我怀疑Groovy没有在你的somethingThatIsNotRule上找到
预期的方法,或者你得到一个ClassCastException或者类似的东西。
collector5b =>与另一个规则5b相同

对于你的悖论,再次, @Rule 注释实际上并不适用于该字段。



我怀疑你的困惑来自于这样一个事实,即Groovy不会抱怨 @Rule 在不是字段或方法的东西上(而Java会)。它可能不会抱怨,但JUnit会忽略这样的注释。


Trying some variants of rules creation in a groovy file, I have come to the thought, that @Rule doesn't describe DECLARATION, but ASSIGNMENT. So, the runner, when loading the test, tries every rule for the correct assignment.

//Correct variants:
@Rule
public ErrorCollector collector1= new ErrorCollector();

public ErrorCollector collector2= null;
@Rule
collector2= new ErrorCollector();

public ErrorCollector collector3;
@Rule
collector3= new ErrorCollector();


// incorrect variants:
@Rule
public ErrorCollector collector4= null;

@Rule
public ErrorCollector collector5;

@Rule
public ErrorCollector collector5=somethingThatIsNotRule;

@Rule
public ErrorCollector collector5=anotherRule;

But, then I came to some paradoxial variants:

//these lines are not only taken by the runner, but also passed without errors:
public ErrorCollector collector6;
{
  @Rule
  collector6= null;
}

public ErrorCollector collector7=null;
{
  @Rule
  collector7= null;
}

What is the logic of it?

It seems as a bug in Runner - the runner makes an excessive check before constructing the test.

解决方案

In Java, the JUnit runner checks that the @Rule annotation is applied to a public non-static field or public non-static method which returns either a TestRule or MethodRule.

If there is @Rule annotation on a field or method, then the value must be a non-null value or you'll get a NullPointerException during execution of the test.

Your example is more complicated than that because Groovy is a dynamic language, so it does its checking at runtime, not compile time. I suspect that collector2 and collector3 aren't actually doing anything. The @Rule annotation doesn't apply to the field.

collector4 => NullPointerException
collector5 => same as collector5
collector5a => when you execute, I suspect Groovy doesn't find the
               expected methods on your somethingThatIsNotRule, or
               you're getting a ClassCastException or something similar.
collector5b => same as 5b for anotherRule

For your paradoxes, again, the @Rule annotation isn't actually applying to the field.

I suspect your confusion comes from the fact that Groovy doesn't complain about the usage of @Rule on something that isn't a field or method (whereas Java would). It may not complain, but JUnit will ignore such an annotation.

这篇关于Groovy类中的@Rule(JUnit)声明和赋值的逻辑是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆