Very simple step by step JBehave setup tutorial?

ε祈祈猫儿з 提交于 2019-11-30 12:02:58

问题


Though I have read many, but many articles on how to use JBehave, I can't get it to work. Here are the steps I went through so far:

  1. Created new Java Project
  2. Downloaded JBehave JAR file version 3.6.8 and added it to my build path libraries
  3. Created a package called com.wmi.tutorials.bdd.stack.specs under the test source folder in my workspace
  4. Added the JBehave JAR file to my Build path Library configuration
  5. Created a JBehave story in the above-mentioned package (StackBehaviourStories.story)
  6. Created a Java class in the above-mentioned package (StackBehaviourStory.java)
  7. Created a Java class in the above-mentioned package (StackBehaviourSteps.java)
  8. Imported the Given, Named, Then, When annotations in my Java class
  9. Written two different scenarios in my JBehave story file

And still, I can't get it to work/run! =(

The story file:

Narrative:
In order to learn to with JBehave using Eclipse
As a junior Java developer though senior in .Net and in BDD
I want to define the behaviour of a custom stack

Scenario: I push an item onto the stack
Given I have an empty stack
When  I push an item 'orange'
Then  I should count 1

Scenario: I pop from the stack
Given I have an empty stack
When  I push an item 'apple'
And   I pop the stack
Then  I should count 0

The story class

package com.wmi.tutorials.bdd.stack.specs

import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.junit.JUnitStory;

public class StackBehaviourStory extends JUnitStory {
    @Override 
    public Configuration configuration() { return new MostUsefulConfiguration(); }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration()
                                      , new StackBehaviourSteps());   
    }
}

The steps class

package com.wmi.tutorials.bdd.stack.specs

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.junit.Assert;

public class StackBehaviourSteps {
    @Given("I have an empty stack")
    public void givenIHaveAnEmptyStack() { stack = new CustomStack(); }

    @When("I push an item $item")
    public void whenIPushAnItem(@Named("item") String item) { stack.push(item); }

    @Then("I should count $expected")
    public void thenIShouldCount(@Named("expected") int expected) {
        int actual = stack.count();
        if (actual != expected) 
            throw new RuntimeException("expected:"+expected+";actual:"+actual);
    }
}

I'm currently using Eclipse Kepler (4.3) JEE with everything I need to use JUnit, Google App Engine, and yes, JBehave is installed correctly following the Eclipse JBehave installation tutorial.

I can't get it to work. So how can I make it work correctly using Eclipse, JBehave and JUnit?


回答1:


I know I'm late to the party here but I'm posting because this is the info I wish I had a week ago as it would've saved me a lot of pain. I'm very much into the idea of BDD, but am unfortunately finding JBehave's docs to be a bit of a nightmare, especially when it comes to Maven integration. Moreover a lot of the code I found both on their website and elsewhere didn't work. Through trial and error, and lots of tutorials, I was able to piece together the following. It runs both in Maven and Eclipse, has a single binding class that maps stories to step files, and is able to find story files located in src/test/resources.

here is a working pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.projectvalis.st1</groupId>
  <artifactId>st1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>st1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <pluginManagement>
      <plugins>

        <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        <compilerArgument></compilerArgument>
          </configuration>
        </plugin>

       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-failsafe-plugin</artifactId>  
         <version>${failsafe.and.surefire.version}</version>  
         <executions>  
           <execution>  
             <id>integration-test</id>  
             <goals>  
               <goal>integration-test</goal>  
               <goal>verify</goal>  
             </goals>  
           </execution>  
         </executions>  
         <configuration>  
           <includes>  
             <include>**/*Test.java</include>  
           </includes>  
         </configuration>  
       </plugin>

       <plugin>
        <groupId>org.jbehave</groupId>
        <artifactId>jbehave-maven-plugin</artifactId>
        <version>4.0.2</version>
            <executions>  
                <execution>  
                    <id>run-stories-as-embeddables</id>  
                    <phase>integration-test</phase>  
                    <configuration>  

                    <includes>  
                        <include>**/*Test.java</include>  
                    </includes>  
                    <ignoreFailureInStories>false</ignoreFailureInStories>  
                    <ignoreFailureInView>false</ignoreFailureInView>  

                        <systemProperties>
                            <property>
                                <name>java.awt.headless</name>
                                <value>true</value>
                            </property>
                        </systemProperties>


                    </configuration>  
                    <goals>  
                    <goal>run-stories-as-embeddables</goal>  
                    </goals>  
            </execution>  
             </executions>
       </plugin>

      </plugins>
    </pluginManagement>
  </build>


  <dependencies>

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

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.0.1</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>

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

  </dependencies>
</project>

here is a sample story file

Narrative:
In order to work with files to compress
As a guy who wants to win a bet with cameron
I want to ensure files are ingested and processed in the manner in which the
methods in the ingest class purport to process them. 

Scenario:  Simple test to give JBehave a test drive
Given a file, a.log
When the caller loads the file as a byte array
Then the byte array that is returned contains the correct number of bytes.

here is a sample step file

package com.projectvalis.compUtils.tests.ingest;

import java.io.File;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.steps.Steps;
import org.junit.Assert;

import com.projectvalis.compUtils.util.fileIO.Ingest;


    /**
     * BDD tests for the ingest class
     * @author funktapuss
     *
     */
    public class LoadByteSteps extends Steps {

        private String fNameS;
        private byte[] byteARR;

        @Given("a file, $filename")
        public void setFileName(@Named("filename") String filename) {
            File file = new File(getClass().getResource("/" + filename).getFile());
            fNameS = file.getPath();
        }

        @When("the caller loads the file as a byte array")
        public void loadFile() {
            byteARR = Ingest.loadFile(fNameS);
        }

        @Then("the byte array that is returned contains the "
                + "correct number of bytes.") 
        public void checkArrSize() {
            File file = new File(fNameS);
            Assert.assertTrue(
                    "loading error - "
                    + "the file and the resultant byte array are different sizes!", 
                    (long)byteARR.length == file.length());
        }


    }

and here is the generic runner

package com.projectvalis.compUtils.tests.runner;


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

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.Steps;

import com.projectvalis.compUtils.tests.ingest.LoadByteSteps;



/**
 * generic binder for all JBehave tests. Binds all the story files to the 
 * step files. works for both Eclipse and Maven command line build.  
 * @author funktapuss
 *
 */
public class JBehaveRunner_Test extends JUnitStories {

    @Override 
    public Configuration configuration() { 
        return new MostUsefulConfiguration()            
                .useStoryLoader(
                        new LoadFromClasspath(this.getClass().getClassLoader()))
                .useStoryReporterBuilder(
                        new StoryReporterBuilder()
                            .withDefaultFormats()
                            .withFormats(Format.HTML, Format.CONSOLE)
                            .withRelativeDirectory("jbehave-report")
                );
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        ArrayList<Steps> stepFileList = new ArrayList<Steps>();
        stepFileList.add(new LoadByteSteps());

        return new InstanceStepsFactory(configuration(), stepFileList);       
    }

    @Override
    protected List<String> storyPaths() {
       return new StoryFinder().
               findPaths(CodeLocations.codeLocationFromClass(
                       this.getClass()), 
                       Arrays.asList("**/*.story"), 
                       Arrays.asList(""));

    }

}

the runner lives in src/test/java//tests.runner. the ingest test lives in src/test/java//tests.ingest. the story files live in src/test/resources/stories.

As far as I can tell, JBehave has LOTS of options, so this certainly isn't the only way of doing things. Treat this like a template that will get you up and running quickly.

full source is on github.




回答2:


Following step by step closely the jbehave Getting Started tutorial, the Run story section says: [...] the ICanToggleACell.java class will allow itself to run as a JUnit test.

This means that the JUnit library is required in your Build path.

Using Eclipse:

  1. Select your current project and right-click it, Build path, Configure Build Path...
  2. Properties for [current project], Java Build Path, Libraries, click [Add Library...]
  3. Add Library, select JUnit, click [Next]
  4. JUnit Library, JUnit library version, select the version you wish to use, click [Finish]
  5. Java Build Path, click [OK]
  6. Project Explorer, select your ICanToggleACell.java class, right-click it, then Run As, and click on JUnit Test

So this is the same here as for the above-example code. The StackBehaviourStory.java class should let itself run as a JUnit test after you add the proper library to the Java build path.




回答3:


In my case, I have extended my Steps class from Steps (from jbehave core)




回答4:


i had updated the JunitStory to JunitStories and it worked

public class StackBehaviourStory extends JUnitStory ---> JunitStories



来源:https://stackoverflow.com/questions/20733703/very-simple-step-by-step-jbehave-setup-tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!