What is the logic of @Rule(JUnit) declaration and assignment in a Groovy class

穿精又带淫゛_ 提交于 2019-12-11 06:32:36

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/15943145/what-is-the-logic-of-rulejunit-declaration-and-assignment-in-a-groovy-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!