Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I asked this question on StackOverflow and have not gotten any answers, so re-asking here.

I am trying to set up JBehave for our school project and am unable to figure out why an Assert.fail() in a step (theResultShouldBe) is causing the error message to display in the console rather than showing a failure in JUnit:

Tests pass but error is displayed in console

Here are the files:

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

Please note, if I comment out the Assert.fail() and re-run, it works as expected:

Tests Pass after commenting out Assert.fail()

and the console displays:

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)
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.