【发布时间】:2018-06-05 13:00:34
【问题描述】:
有人可以举例说明 beforeScenario 和 afterScenario 在 JBehave 中的工作原理吗?
我创建了一个类,其中包含两个方法 gearUp 和 @BeforeScenario 和 tearDown 和 @AfterScenario 注释。
但这些方法在 JBehave 中从未被调用。
需要什么额外的配置。任何代码示例都会对我们有所帮助。
而 Cucumber 中的这种简单整洁。
以下是我的单步故事文件(src/test/resources/storeis):
Scenario: SampleTest
Given I am test
以下是我的步骤文件
public class jbehavetc {
@Given("I am test")
public void startOnUrl(String url) {
System.out.println("I am actual test");
}
}
以下是我的 Hooks 文件,其中包含 BeforeScenario 和 AfterScenario 方法
public class Hooks {
@BeforeScenario
public void startSystem() throws Exception {
System.out.println("I am before scenario");
}
@AfterScenario
public void stopSystem() throws Exception {
System.out.println("I am after scenario");
}
}
为了运行上面的故事,我创建了一个运行器文件并希望作为 JUnit 测试运行(纠正我这不是正确的方法)
public class JBehaveRunner extends JUnitStory{
@Override
public Configuration configuration() {
return new MostUsefulConfiguration()
.useStoryLoader(
new LoadFromClasspath(getClass().getClassLoader()))
.useStoryReporterBuilder(
new StoryReporterBuilder()
.withDefaultFormats()
.withFormats(Format.HTML));
}
@Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(), new jbehavetc(),
new Hooks());
}
public List<String> storyPaths() {
return new StoryFinder().findPaths(
CodeLocations.codeLocationFromClass(this.getClass()),
Arrays.asList("**/*.story"),
Arrays.asList(""));
}
@Test
public void run() throws Throwable {
super.run();
}
}
当我作为 JUnit 测试在 runner 上运行时,什么都没有执行。我怎样才能超越故事?我希望在运行此运行程序或故事文件时需要调用场景方法之前和之后。
【问题讨论】:
-
在提供的示例方法中,
storyPaths将被忽略,因为JUnitStory已扩展。扩展JUnitStories或通过Configuration#useStoryPathResolver配置故事路径解析器
标签: jbehave