2014-08-31 90 views
0

我是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测试运行”时,一切都很顺利。然而,当我把@RunWith(JUnitReportingRunner.class)在我的故事类的顶部,需要JBehave-的JUnit亚军,我得到以下错误:JBehave-Junit-Runner投掷NullPointerException

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"); 
    } 
} 

t中是否有任何明显的错误他编码,还是应该退出使用beta库?

+0

乍一看,代码看起来很好。 我还没有尝试过4.0版本。同一个例子是否与稳定版本的JBehave一起工作?如果是的话,我想问你,在github上跟踪器中为此创建一个问题:https://github.com/codecentric/jbehave-junit-runner/issues – AndreasEK 2014-09-01 05:04:57

+0

你可以尝试的一件事是使用 JUnitReportingRunner.recommandedControls(configuredEmbedder()); 在你的构造函数中作为最后一行,如下所示: https://github.com/codecentric/jbehave-junit-runner#enabling – AndreasEK 2014-09-01 05:15:11

+0

谢谢你的建议Andreas。我已经回答了我的问题,以解释您的哪些建议对我有用,并根据您的要求在GitHub(#70)提出了一个问题。 – Flic 2014-09-03 14:37:54

回答

2

我发现问题出在JUnit-4.12-beta-1上。我将我的Gradle构建脚本设置为4. +,所以我将其更改为指定4.11,问题消失。 JBehave核心4.0-beta-9似乎工作得很好,所以我放弃了这一点。我也尝试过使用JUnitReportingRunner.recommandedControls(configuredEmbedder());我试过使用JUnitReportingRunner.recommandedControls(configuredEmbedder());作为构造函数的最后一行,但它实际上抛出了一个额外的错误。

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

亲切的问候, Flic

+0

这对我有用,但我也必须确保jbehave-junit-runner是最新版本(以前是1.0.1;升级到1.1.2)。 JBehave核心4.0-beta-10也看起来不错,但会保持锁定在该版本并谨慎升级。 – RCross 2014-09-10 10:56:15