我知道我迟到了这里的聚会,但我发布是因为这是我希望我有一个星期前的信息,因为它会给我带来很多痛苦.我非常喜欢BDD的想法,但不幸的是发现JBehave的文档是一场噩梦,特别是当涉及到Maven集成时.此外,我在他们的网站和其他地方发现的很多代码都没有起作用.通过尝试和错误,还有很多教程,我能够整理下列内容.它在Maven和Eclipse中运行,具有将故事映射到步骤文件的单个绑定类,并且能够查找位于src / test / resources中的故事文件.
这里是一个工作的pom文件:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
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.