gpt4 book ai didi

java - @Rule 声明会导致调试尝试出错

转载 作者:行者123 更新时间:2023-12-01 23:53:45 25 4
gpt4 key购买 nike

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

@Rule
public static ErrorCollector errorCollector;

尝试在 Debug模式下启动测试后,出现异常:

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

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

最佳答案

正如您在 ErrorCollector javadoc 中看到的那样,您必须创建一个实例才能使用它,如下所示:

    @Rule
public ErrorCollector collector= new ErrorCollector();

关于java - @Rule 声明会导致调试尝试出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com