我是新的 Jbehave。我试图找到一种通过 Jbehave 实现黄瓜世界的方法。我使用 jbehave 和 pico 原型生成了项目。我试图并行运行两个故事。我将 pom.xml 中的线程更新为 2。考虑如下示例 stepdef 类
public class PojoSteps {
public Pojo pojo;
public PojoSteps(Pojo pojo){
this.pojo = pojo;
System.out.println(" @@" + new Timestamp(System.currentTimeMillis()) + " * this is " + this + ". In PojoSteps constructor. Pojo created is:" + this.pojo + ". Thread = " + Thread.currentThread().getId());
}
@Given("I have a pojo with $i")
public void iHaveAPojoWith1(int i){
pojo.setI(i);
System.out.println(" @@" + new Timestamp(System.currentTimeMillis()) + " * this is " + this + ".In iHaveAPojoWith1. pojo = " + this.pojo +". Value of To be set = " + i +". Value set Pojo.getI() = " + this.pojo.getI() + ". Thread = " + Thread.currentThread().getId());
}
@When("I wait for some time")
public void randomWait() throws InterruptedException {
Thread.sleep(new Random().nextInt(200));
}
@Then("my pojo should still have $expectedi")
public void myPojoShouldStillHave1(int expectedi){
System.out.println(" @@" + new Timestamp(System.currentTimeMillis()) + " * this is " + this +". In myPojoShouldStillHave1. pojo = " + this.pojo + ". expected = " + expectedi + ". actual = " + this.pojo.getI() + ". Thread = " + Thread.currentThread().getId());
Assert.assertEquals(this.pojo.getI(), expectedi);
}
}
我有如下 Pojo 模型
public class Pojo {
private int i;
public void setI(int i){
this.i = i;
}
public int getI(){
return this.i;
}
}
我的两个故事如下 PojoOne.story
Scenario: scenario description
Given I have a pojo with 1
When I wait for some time
Then my pojo should still have 1
PojoTwo.story
Scenario: random description
Given I have a pojo with 2
When I wait for some time
Then my pojo should still have 2
我有 MyStories 类,它扩展了 JUnitStories,如下所示
public class MyStories extends JUnitStories {
....
private PicoContainer createPicoContainer() {
MutablePicoContainer container = new DefaultPicoContainer(new Caching().wrap(new ConstructorInjection()));
container.addComponent(PojoSteps.class);
container.addComponent(Pojo.class);
return container;
}
}
当我不并行运行故事时,它们会成功。当我并行运行故事时,它们失败了。
@@2020-01-02 20:35:36.53 * this is learning.steps.PojoSteps@49f3f232. In PojoSteps constructor. Pojo created is:learning.Support.Pojo@4aa9e824. Thread = 12
@@2020-01-02 20:35:36.531 * this is learning.steps.PojoSteps@49f3f232.In iHaveAPojoWith1. pojo = learning.Support.Pojo@4aa9e824. Value of To be set = 1. Value set Pojo.getI() = 1. Thread = 12
@@2020-01-02 20:35:36.532 * this is learning.steps.PojoSteps@6136e035. In PojoSteps constructor. Pojo created is:learning.Support.Pojo@1f4412c8. Thread = 13
@@2020-01-02 20:35:36.533 * this is learning.steps.PojoSteps@6136e035.In iHaveAPojoWith1. pojo = learning.Support.Pojo@1f4412c8. Value of To be set = 2. Value set Pojo.getI() = 2. Thread = 13
@@2020-01-02 20:35:36.558 * this is learning.steps.PojoSteps@6136e035. In myPojoShouldStillHave1. pojo = learning.Support.Pojo@1f4412c8. expected = 1. actual = 2. Thread = 12
@@2020-01-02 20:35:36.668 * this is learning.steps.PojoSteps@6136e035. In myPojoShouldStillHave1. pojo = learning.Support.Pojo@1f4412c8. expected = 2. actual = 2. Thread = 13
...
Then my pojo should still have 1 (FAILED)
(java.lang.AssertionError: expected:<2> but was:<1>)
我找不到有关如何与 Jbehave 和 pico 并行运行测试的文档。此外,我需要在我的 stepdef 类之外有一个模型,因为我将拥有多个共享同一个模型实例的 stefdef 类。