为什么ErrorCollector在声明时要求分配? [英] Why does ErrorCollector demands for assignment at the moment of declaration?

查看:352
本文介绍了为什么ErrorCollector在声明时要求分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查许多XML字符串,我经常使用ErrorCollector构造。但我仍然不明白它是如何工作的。

Checkinging many XML strings, I use the ErrorCollector construction frequently. But still I do not understand how it works.

当我声明一个ErrorCollector时,我必须立即分配它:

When I am declaring an ErrorCollector, I have to assign it at once:

@Rule
public ErrorCollector collector= new ErrorCollector();

如果我想在每次测试之前刷新收藏家,我正在进行作业

If I want to refresh the collector before every test, I am putting an assignment

collector= new ErrorCollector();

。但因此,宣言的第一项任务是过分的。但是我无法删除它。

in the @Before method. But thus the first assignment at the declaration is excessive. But I can't remove it.

这个必须分配的意义是什么?它是如何工作的?我想,@ Rule仅仅是关于声明的吗?

What is the sense of this must-to-be-assignment? And how it works? I thought, that @Rule is about the declaration only?

@Matthew Farwell 什么是@Rule(JUnit)声明和赋值在Groovy类中的逻辑说在Java中,JUnit运行器检查@Rule注释是否应用于公共非静态字段或公共非静态方法,它返回TestRule或MethodRule。但是这里检查的不是声明,而是赋值,它是在构建和测试构建之后发生的事情吗?

@Matthew Farwell at What is the logic of @Rule(JUnit) declaration and assignment in a Groovy class says "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." But what is checked here is not the declaration, but the assignment and it is something that happens afterwards, after both building and test construction ?

推荐答案

与JUnit中的大多数/所有 @Rule 一样,它们是刷新在每次测试之前自动清除。因此,您只需将其声明为 @Rule ,如上所示。您不需要在 @Before 方法中对其执行任何操作。我们想到的是JUnit将在每次测试之前和之后调用 ErrorCollector 。在每次测试之前, ErrorCollector 可以自行初始化(清除以前存储的任何错误)。每次测试后,如果收集到错误,它会报告错误。

As with most / all @Rules in JUnit, they are refreshed or cleared before each test automatically. So you just declare it as a @Rule as you have listed above. You DO NOT need to do anything to it in your @Before method. We way to think of it is that JUnit will invoke the ErrorCollector before and after each test. Before each test the ErrorCollector can initialize itself (clearing any previously stored errors). After each test, if an error has been collected it reports the error(s).

以下是 ErrorCollector

来自评论:

必须分配它,因为JUnit必须具有要使用的规则的实例。否则该对象将为null。请记住,JUnit代码使用反射来获取带有@Rule注释的Object实例,并使用instanceof来确定它是TestRule还是MethodRule。我相信在声明中调用@Before方法,因此在调用规则之后

It must be assigned because JUnit must have an instance of the rule to work with. Otherwise the object would be null. Remember that the JUnit code is using reflection to get the instance of the Object with the @Rule annotation and using instanceof to determine if it is a TestRule or MethodRule. I believe the @Before method is called within the Statement and therefore after the Rule is invoked

FROM MatthewFarwell:

FROM MatthewFarwell:

当您运行JUnit测试(例如,FooTest)时,运行器将为每个测试方法创建一个FooTest实例。所以如果你正在使用公共的ErrorCollector collector = new ErrorCollector();如上所述,每次测试方法自然会重新实例化一次。

When you run a JUnit test, for example, FooTest, then the runner will create an instance of FooTest for each test method. So if you're using public ErrorCollector collector= new ErrorCollector(); as above, then this gets naturally re-instantiated once per test method.

结论:确保不在测试之间保存状态!当然,你不应该这样做,因为订单无法保证。

Conclusing: Make sure that you don't save state between your tests! Of course you shouldn't have done that anyway since order is not guaranteed.

谢谢Matt!

这篇关于为什么ErrorCollector在声明时要求分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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