2014-03-30 67 views
1

我試圖建立JBehave爲我們的學校項目,但無法弄清楚爲什麼在步驟的Assert.fail()theResultShouldBe)導致該錯誤消息顯示在控制檯而不是呈現出故障的JUnitJBehave Assert.fail未能在控制檯,而不是在JUnit中(日蝕)

Tests pass but error is displayed in console

以下是文件:

src/main/resources/stories/edu/cmu/SmartParks/jbehave/add_scenarios.story

Narrative: 
    In order to test JBehave 
    As a development team 
    I want to add two numbers 

    Scenario: Add two numbers 
    Given the number 10 
    And the number 72 
    When the numbers are added 
    Then the result is 82 

src/test/java/edu/cmu/SmartParks/jbehave/AddSteps.java

package edu.cmu.SmartParks.jbehave; 

import org.jbehave.core.annotations.*; 
import org.jbehave.core.embedder.Embedder; 
import junit.framework.Assert; 

public class AddSteps extends Embedder 
{ 

    private int x; 
    private int y; 
    private int z; 

    @Given("the number $num") 
    public void number(int num) 
    { 
     if(x == 0) 
     { 
      x = num; 
     } 
     else 
     { 
      y = num; 
     } 
    } 

    @When("the numbers are added") 
    public void addNumbers() 
    { 
     z = x+y; 
    } 

    @Then("the result is $result") 
    public void theResultShouldBe(int result) 
    { 
     Assert.assertEquals(z, result); 
     Assert.fail(); 
    } 
} 

src/test/java/edu/cmu/SmartParks/jbehave/AddScenarios.java

package edu.cmu.SmartParks.jbehave; 


import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.List; 

import org.jbehave.core.configuration.Configuration; 
import org.jbehave.core.configuration.MostUsefulConfiguration; 
import org.jbehave.core.io.LoadFromRelativeFile; 
import org.jbehave.core.junit.JUnitStory; 
import org.jbehave.core.reporters.StoryReporterBuilder; 
import org.jbehave.core.reporters.StoryReporterBuilder.Format; 
import org.jbehave.core.steps.CandidateSteps; 
import org.jbehave.core.steps.InstanceStepsFactory; 
import junit.framework.Test; 

public class AddScenarios extends JUnitStory { 

     @Override 
     public Configuration configuration() { 
      URL storyURL = null; 
      try { 
       // This requires you to start Maven from the project directory 
       storyURL = new URL("file://" + System.getProperty("user.dir") 
         + "/src/main/resources/stories/"); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } 
      return new MostUsefulConfiguration().useStoryLoader(
        new LoadFromRelativeFile(storyURL)).useStoryReporterBuilder(
        new StoryReporterBuilder().withFormats(Format.HTML)); 
     } 

     @Override 
     public List<CandidateSteps> candidateSteps() { 
      return new InstanceStepsFactory(configuration(), new AddSteps()) 
        .createCandidateSteps(); 
     } 

     @Override 
     @org.junit.Test 
     public void run() { 
      try { 
       super.run(); 
      } catch (Throwable e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

error message

Processing system properties {} 
Running story edu/cmu/SmartParks/jbehave/add_scenarios.story 
org.jbehave.core.embedder.Embedder$RunningStoriesFailed: Failures in running stories edu/cmu/SmartParks/jbehave/add_scenarios.story 
    at org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:211) 
    at org.jbehave.core.junit.JUnitStory.run(JUnitStory.java:23) 
    at edu.cmu.SmartParks.jbehave.AddScenarios.run(AddScenarios.java:45) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    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:50) 
    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) 
Caused by: junit.framework.AssertionFailedError: null 
    at junit.framework.Assert.fail(Assert.java:47) 
    at junit.framework.Assert.fail(Assert.java:53) 
    at edu.cmu.SmartParks.jbehave.AddSteps.theResultShouldBe(AddSteps.java:46) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.jbehave.core.steps.StepCreator$5.perform(StepCreator.java:132) 
    at org.jbehave.core.embedder.StoryRunner$FineSoFar.run(StoryRunner.java:256) 
    at org.jbehave.core.embedder.StoryRunner.runStepsWhileKeepingState(StoryRunner.java:244) 
    at org.jbehave.core.embedder.StoryRunner.runScenarioSteps(StoryRunner.java:235) 
    at org.jbehave.core.embedder.StoryRunner.run(StoryRunner.java:149) 
    at org.jbehave.core.embedder.StoryRunner.run(StoryRunner.java:85) 
    at org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:202) 
    ... 25 more 

請注意,如果我註釋掉Assert.fail()並重新運行,它活像KS如預期:

Tests Pass after commenting out Assert.fail()

和控制檯顯示:

Processing system properties {} 
Running story edu/cmu/SmartParks/jbehave/add_scenarios.story 
Generating reports view to '<PATHTOPROJECT>\target\jbehave' using formats '[html]' and view properties '{viewDirectory=view, views=ftl/jbehave-views.ftl, reports=ftl/jbehave-reports-with-totals.ftl, decorateNonHtml=true, nonDecorated=ftl/jbehave-report-non-decorated.ftl, decorated=ftl/jbehave-report-decorated.ftl, defaultFormats=stats, maps=ftl/jbehave-maps.ftl}' 
Reports view generated with 1 stories containing 0 scenarios (of which 0 failed) 

回答

3

我面臨着同樣的問題,jbehave-junit-runner固定的問題。

只需將依賴項添加到您的pom並將註釋添加到JUnitStories類(如README中所述)。

UPDATE:確保在配置類中使用doIgnoreFailureInStories(false)和doIgnoreFailureInView(false)。這足以解決我的問題,但上面的插件在JUnit選項卡中提供了額外的細節,因此我仍然建議使用它。

+1

儘管此鏈接可能會回答問題,但最好在此處包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – Muhammed

+1

我已更新答案以提供另一種可能的解決方案。 – RamiroOliva

相關問題