2013-04-10 29 views
0

一個Groovy JUnit測試類只有一個靜態聲明錯誤:@rule聲明導致在調試嘗試

@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

我應該在哪裏尋找問題?

+2

你不應該創建'ErrorCollector'的實例嗎?像:公共靜態ErrorCollector errorCollector = new ErrorCollector();'。見http://javasourcecode.org/html/open-source/junit/junit-4.8.1/org/junit/rules/ErrorCollector.html – 2013-04-10 17:04:47

+0

@JoseRenato它甚至在javadoc。添加一個答案,我會投票。 – 2013-04-10 18:25:35

+0

@MatthewFarwell它在那裏。 – 2013-04-10 18:30:17

回答

3

正如你可以在ErrorCollector javadoc看到,你必須創建一個實例來使用它,像這樣:

@Rule 
    public ErrorCollector collector= new ErrorCollector(); 
+0

+1如上所述。 – 2013-04-10 19:44:17

+0

它的工作原理。但我不明白,爲什麼。在javadoc中顯示的可能性並不意味着我們必須使用它,是嗎?我會提出另一個問題。非常感謝大家 – Gangnus 2013-04-11 06:58:47

+0

你現在的代碼如何?你刪除了'static'修飾符?或者只是創建了'ErrorCollector'的實例? – 2013-04-11 13:27:40

0

似乎在亞軍的錯誤:

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

所以,亞軍品牌在構建測試之前進行過度檢查。