2014-08-31 41 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