Google

Oct 17, 2013

jBehave and jUnit for BDD

This extends the jBehave -- Behavioural Driven Development (BDD) to use the same example with jUnit testing.

Step 1:  Make sure that you have both jUnir and jBehave in your pom.xml file.

 
<dependency>
 <groupId>org.jbehave</groupId>
 <artifactId>jbehave-core</artifactId>
 <version>3.8</version>
</dependency>

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.10</version>
 <scope>test</scope>
</dependency>


Step 2: Refer to Behavioural Driven Development (BDD) for all the jBehave implementation.

Step 3: The jUnit class with jBehave. It extends the  JUnitStories class.

 
package com.mycompany.jbehave;

import java.util.Arrays;
import java.util.List;

import org.jbehave.core.junit.JUnitStories;

public class JBehaveUnitTest extends JUnitStories
{
    
    public JBehaveUnitTest()
    {
        super();
        this.configuredEmbedder().candidateSteps().add(new MathSteps());
    }
    
    @Override
    protected List<string>> storyPaths()
    {
        return Arrays.asList("jbehave/math.story");
    }
    
}


Step 4: You can run the above class as jUnit, but won't give you enough info and not very user friendly. This is where the jBehave jUnit extansion becomes very handy. This can be added to your maven pom.xml file as shown below.

 
<dependency>
 <groupId>de.codecentric</groupId>
 <artifactId>jbehave-junit-runner</artifactId>
 <version>1.0.2</version>
</dependency>


Step 5: Now, the revised jUnit test class from Step 3.

 
package com.mycompany.jbehave;

import de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner;

import java.util.Arrays;
import java.util.List;

import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.junit.runner.RunWith;

@RunWith(JUnitReportingRunner.class)
public class JBehaveUnitTest extends JUnitStories
{
    
    public JBehaveUnitTest()
    {
        super();
    }
    
    public InjectableStepsFactory stepsFactory()
    {
        return new InstanceStepsFactory(configuration(), new MathSteps());
    }
    
    @Override
    protected List<String> storyPaths()
    {
        return Arrays.asList("jbehave/math.story");
    }
    
}

Step 6: Running it as jUnit within eclipse looks like.



Isn't it great? Communication is the key to jBehave as the scenarios are written in simple English that business and developers can work together.

Labels: ,

1 Comments:

Anonymous Anonymous said...

Hello,

I am receiving the following error when running as JUnit Test. can you provide some pointers?

Reports view generated with 0 stories (of which 0 pending) containing 0 scenarios (of which 0 pending)
java.lang.RuntimeException: org.jbehave.core.embedder.Embedder$RunningStoriesFailed: Failures in running stories:
jbehave/math.story: org.jbehave.core.embedder.StoryManager$StoryExecutionFailed: jbehave/math.story
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: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: org.jbehave.core.embedder.Embedder$RunningStoriesFailed: Failures in running stories:
jbehave/math.story: org.jbehave.core.embedder.StoryManager$StoryExecutionFailed: jbehave/math.story
at org.jbehave.core.embedder.Embedder$ThrowingRunningStoriesFailed.handleFailures(Embedder.java:520)
at org.jbehave.core.embedder.Embedder.handleFailures(Embedder.java:228)
at org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:206)
at de.codecentric.jbehave.junit.monitoring.JUnitReportingRunner.run(JUnitReportingRunner.java:78)
... 6 more

7:20 AM, March 27, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home