首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JBehave Runner掷NullPointerException

JBehave Runner掷NullPointerException
EN

Stack Overflow用户
提问于 2014-08-31 07:46:43
回答 1查看 3.3K关注 0票数 0

我是JBehave新手,并试图使用JUnit Runner来很好地显示Eclipse露娜中的JUnit测试结果(在Ubuntu12.04上)。我使用的是Runner1.1.2,JUnit 4.12-beta-1和JBehave 4.0-beta-9.当我右键点击我的故事文件和‘运行作为JUnit测试’,一切都是好的。但是,当我将@RunWith(JUnitReportingRunner.class)按JUnit Runner的要求放置在我的故事类的顶部时,我会得到以下错误:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
package org.felimar;

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

JBehave的故事文件:

代码语言:javascript
复制
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|

步骤类:

代码语言:javascript
复制
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);
  }
}

最后,故事课:

代码语言:javascript
复制
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");
  }
}

在任何代码中是否存在明显的错误,还是应该从使用beta库中退一步?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-03 14:22:59

我发现问题在于JUnit-4.12-beta-1。我将我的Gradle构建脚本设置为4.+,所以我将它更改为指定了4.11,问题就消失了。JBehave 4.0-beta-9似乎工作得很好,所以我把它放在了原处。

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

我感谢安德烈亚斯的有益建议,他们非常感谢,并最终帮助我解决了我的问题。

亲切的问候,福丽

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25589963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档