一步一步的JBehave设置教程非常简单?

虽然我已经阅读了许多关于如何使用JBehave的文章,但我无法让它工作。 这是我到目前为止所经历的步骤:

  1. 创建了新的Java Project
  2. 下载了JBehave JAR文件版本3.6.8并将其添加到我的构建路径库中
  3. 在我的工作区中的测试源文件夹下创建了一个名为com.wmi.tutorials.bdd.stack.specs的包
  4. 将JBehave JAR文件添加到我的构建路径库配置中
  5. 在上述包中创建了一个JBehave故事(StackBehaviourStories.story)
  6. 在上面提到的包(StackBehaviourStory.java)中创建了一个Java类
  7. 在上面提到的包(StackBehaviourSteps.java)中创建了一个Java类
  8. 导入了我的Java类中的Given,Named,Then,When annotations
  9. 在我的JBehave故事文件中写了两个不同的场景

仍然,我无法让它工作/运行! =(

故事档案:

 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 

故事课

 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()); } } 

步骤类

 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); } } 

我目前正在使用Eclipse Kepler(4.3)JEE以及使用JUnit,Google App Engine所需的一切,是的,按照Eclipse JBehave安装教程正确安装了JBehave。

我无法让它发挥作用。 那么如何使用Eclipse,JBehave和JUnit使其正常工作?

我知道我在这里参加派对已经很晚了,但是我发帖是因为这是我希望我一周以前的信息,因为它会给我带来很多痛苦。 我非常关注BDD的想法,但不幸的是,发现JBehave的文档有点像噩梦,特别是在涉及到Maven集成时。 此外,我在他们的网站和其他地方发现的很多代码都不起作用。 通过反复试验和许多教程,我能够拼凑出以下内容。 它在Maven和Eclipse中运行,有一个将故事映射到步骤文件的绑定类,并且能够找到位于src / test / resources中的故事文件。

这是一个工作的pom文件:

  4.0.0 com.projectvalis.st1 st1 1.0-SNAPSHOT jar st1 http://maven.apache.org  UTF-8      maven-compiler-plugin 2.3.2  1.8 1.8     org.apache.maven.plugins maven-failsafe-plugin ${failsafe.and.surefire.version}   integration-test  integration-test verify      **/*Test.java     org.jbehave jbehave-maven-plugin 4.0.2   run-stories-as-embeddables integration-test   **/*Test.java  false false   java.awt.headless true     run-stories-as-embeddables          junit junit 4.12 test   org.slf4j slf4j-api 1.7.7   ch.qos.logback logback-classic 1.0.1   ch.qos.logback logback-core 1.0.1   org.apache.commons commons-lang3 3.4   org.jbehave jbehave-core 4.0.2    

这是一个示例故事文件

 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. 

这是一个示例步骤文件

 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()); } } 

这是通用的跑步者

 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 stepFileList = new ArrayList(); stepFileList.add(new LoadByteSteps()); return new InstanceStepsFactory(configuration(), stepFileList); } @Override protected List storyPaths() { return new StoryFinder(). findPaths(CodeLocations.codeLocationFromClass( this.getClass()), Arrays.asList("**/*.story"), Arrays.asList("")); } } 

运行器位于src / test / java / /tests.runner中。 摄取测试存在于src / test / java / /tests.ingest中。 故事文件存在于src / test / resources / stories中。

据我所知,JBehave有很多选择,所以这当然不是唯一的做法。 将此视为一个模板,可以让您快速启动并运行。

完整的源代码在github上 。

紧跟jbehave Getting Started教程一步一步, 运行故事部分说: […] ICanToggleACell.java类将允许自己作为JUnit测试运行。

这意味着构建路径中需要JUnit库。

使用Eclipse:

  1. 选择当前项目并右键单击它, 构建路径配置构建路径…
  2. [当前项目]的属性, Java Build PathLibraries ,单击[Add Library …]
  3. 添加库,选择JUnit ,单击[下一步]
  4. JUnit Library,JUnit库版本,选择您要使用的版本,单击[Finish]
  5. Java Build Path,单击[确定]
  6. 在Project Explorer中,选择您的ICanToggleACell.java类,右键单击它,然后运行为,然后单击JUnit Test

因此,这与上面的示例代码相同。 在将适当的库添加到Java构建路径之后, StackBehaviourStory.java类应该让自己作为JUnit测试运行。

就我而言,我已经从Steps(从jbehave核心)扩展了我的Steps类

我已将JunitStory更新为JunitStories并且它有效

公共类StackBehaviourStory扩展了JUnitStory —> JunitStories