gpt4 book ai didi

java - JBehave-Junit-Runner 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:44:09 25 4
gpt4 key购买 nike

我是 JBehave 的新手,正在尝试使用 JBehave-JUnit-Runner 在 Eclipse Luna(在 Ubuntu 12.04 上)的 JUnit 中很好地显示测试结果。我正在使用 JBehave-JUnit-Runner 1.1.2、JUnit 4.12-beta-1 和 JBehave-core 4.0-beta-9。当我右键单击我的故事文件并“作为 JUnit 测试运行”时,一切都很好。但是,当我按照 JBehave-JUnit-Runner 的要求将 @RunWith(JUnitReportingRunner.class) 放在故事类的顶部时,出现以下错误:

java.lang.RuntimeException: java.lang.NullPointerException
at de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner.run(JUnitReportingRunner.java:80)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
at de.codecentric.jbehave.junit.monitoring.JUnitScenarioReporter.afterStory(JUnitScenarioReporter.java:114)
at org.jbehave.core.reporters.DelegatingStoryReporter.afterStory(DelegatingStoryReporter.java:49)
at org.jbehave.core.reporters.ConcurrentStoryReporter.afterStory(ConcurrentStoryReporter.java:120)
at org.jbehave.core.embedder.PerformableTree.performBeforeOrAfterStories(PerformableTree.java:399)
at org.jbehave.core.embedder.StoryManager.performStories(StoryManager.java:102)
at org.jbehave.core.embedder.StoryManager.runStories(StoryManager.java:93)
at org.jbehave.core.embedder.StoryManager.runStoriesAsPaths(StoryManager.java:74)
at org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:204)
at de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner.run(JUnitReportingRunner.java:78)
... 6 more

这是我用于测试的实用程序类。一种方法,非常基本:

package org.felimar;

public abstract class StringManipulation
{
public static boolean stringBlank(final String src)
{
return src.matches("^\\s*$"); //$NON-NLS-1$
}
}

JBehave 的故事文件:

Utilities for managing character strings

Narrative:
In order to easily manipulate and investigate character strings
As a development team
I want to use a group of string-related utilities

Scenario: A string contains zero or more characters
Given a source string with value <value>
Then the method should return <return>

Examples:
|value|return|
|""|true|
|" "|true|
|"Normal Non-Blank"|false|

步骤类:

package org.felimar.steps;

import static org.felimar.StringManipulation.stringBlank;

import org.felimar.StringManipulation;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;

public class StringManipulationSteps
{
private String m_srcString;

public String getSrcString()
{
return m_srcString;
}

@Given("a source string with value $value")
public void givenValue(@Named("value") final String srcString)
{
setSrcString(srcString);
}

public void setSrcString(final String srcString)
{
m_srcString = srcString;
}

@Then("the method should return $value")
public void stringBlankRtrns(@Named("value") final boolean isBlank)
{
if (stringBlank(getSrcString()) != isBlank)
throw new RuntimeException("stringBlank did not determine *" +
getSrcString() + "* was " + isBlank);
}
}

最后是故事课:

package org.felimar.stories;

import static java.util.Arrays.asList;
import static org.jbehave.core.reporters.Format.CONSOLE;
import static org.jbehave.core.reporters.Format.TXT;

import java.util.List;

import org.felimar.StringManipulation;
import org.felimar.steps.StringManipulationSteps;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.junit.runner.RunWith;

import de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner;

@RunWith(JUnitReportingRunner.class)
public class StringManipulationStories extends JUnitStories
{
public StringManipulationStories()
{
super();
super.useConfiguration(
new MostUsefulConfiguration().useStoryReporterBuilder(
new StoryReporterBuilder().withDefaultFormats().withFormats(
CONSOLE, TXT)));
}

@Override
public InjectableStepsFactory stepsFactory()
{
return new InstanceStepsFactory(configuration(),
new StringManipulationSteps());
}

@Override
protected List<String> storyPaths()
{
return asList("org/felimar/stories/StringManipulationStories.story");
}
}

任何代码中是否存在任何明显的错误,或者我应该停止使用测试版库吗?

最佳答案

我发现问题出在 JUnit-4.12-beta-1 上。我将 Gradle 构建脚本设置为 4.+,因此我将其更改为指定 4.11,问题就消失了。 JBehave-core 4.0-beta-9 似乎工作得很好,所以我将其保留在原处。

我还尝试使用 JUnitReportingRunner.recommandedControls(configuredEmbedder());作为构造函数的最后一行,但它实际上抛出了一个额外的错误。

感谢 Andreas 提供的有用建议 - 他们非常感激并最终帮助我解决了我的问题。

亲切的问候,弗利克

关于java - JBehave-Junit-Runner 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25589963/

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