Java @规则声明导致调试尝试出错

Java @规则声明导致调试尝试出错,java,eclipse,groovy,junit,junit-rule,Java,Eclipse,Groovy,Junit,Junit Rule,groovy JUnit测试类只有一个静态声明: @Rule public static ErrorCollector errorCollector; 尝试在调试模式下启动测试后,将引发异常: java.lang.NullPointerException at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:354) at org.junit.runners.BlockJUnit4Cl

groovy JUnit测试类只有一个静态声明:

@Rule
public static ErrorCollector errorCollector;
尝试在调试模式下启动测试后,将引发异常:

java.lang.NullPointerException
at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:354)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:270)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
在代码中的任何一行开始之前引发异常。 如果我扔掉“@Rule”字,测试运行正常(至少从一开始)

进口产品包括:

import static org.hamcrest.CoreMatchers.*;
import org.hamcrest.Matcher;
import static org.hamcrest.Matchers.*;

import org.junit.Rule;
import org.junit.rules.ErrorCollector;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;

import groovy.util.slurpersupport.NodeChild
import groovy.xml.MarkupBuilder
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
import org.codehaus.groovy.tools.xml.DomToGroovy

import org.joda.time.DateTime
import org.w3c.dom.Document;

import java.util.concurrent.Callable;
JUnit版本4.8.2

Eclipse版本:3.6

Java版本:1.6.41

请问我应该在哪里查找问题?

正如您在上看到的,您必须创建一个实例来使用它,如下所示:

    @Rule
    public ErrorCollector collector= new ErrorCollector();
正如您在上所看到的,您必须创建一个实例才能使用它,如下所示:

    @Rule
    public ErrorCollector collector= new ErrorCollector();

这似乎是Runner中的一个bug:

//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:
//the runner takes the following, and runs it without problems, too:

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

因此,运行程序在构建测试之前进行了过度检查。

这似乎是运行程序中的一个错误:

//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:
//the runner takes the following, and runs it without problems, too:

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

因此,运行程序在构建测试之前进行了过度检查。

您是否应该创建
ErrorCollector
的实例?比如:
publicstaticerrorcollector=newerrorcollector()。请看@JoseRenato,它甚至在javadoc中。添加一个答案,我来投票。@MatthewFarwell它就在那里。你不应该创建一个
ErrorCollector
的实例吗?比如:
publicstaticerrorcollector=newerrorcollector()。请看@JoseRenato,它甚至在javadoc中。加上一个答案,我就投赞成票。@MatthewFarwell它就在那里,很有效。但我不明白,为什么。javadocs中显示的可能性并不意味着我们必须使用它,是吗?我再问一个问题。非常感谢大家现在代码是什么?是否删除了
静态
修改器?或者只是创建了
ErrorCollector
的实例?它可以工作。但我不明白,为什么。javadocs中显示的可能性并不意味着我们必须使用它,是吗?我再问一个问题。非常感谢大家现在代码是什么?是否删除了
静态
修改器?还是刚刚创建了
ErrorCollector
的实例?