Question

A groovy JUnit test class has only one static declaration:

@Rule
public static ErrorCollector errorCollector;

After an attempt to launch the test in debug mode an exception raises:

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)

The exception raises before any line in the code is started. If I throw away the "@Rule" word, the test is running OK (at least from the start)

Imports are:

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 version 4.8.2

Eclipse version: 3.6

Java version: 1.6.41

Where should I look for problems, please?

Was it helpful?

Solution

As you can see at ErrorCollector javadoc, you must create a instance to use it, like this:

    @Rule
    public ErrorCollector collector= new ErrorCollector();

OTHER TIPS

It seems as a bug in Runner:

//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;
}

So, the runner makes an excessive check before constructing the test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top