我是新来的。我正在试图找到一种方法来实现黄瓜与Jbehave的世界。我生成了带有jbehave和pico原型的项目。我正在尝试并行运行两个故事。我将pom.xml中的线程更新为2。
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 1PojoTwo.story
Scenario: random description
Given I have a pojo with 2
When I wait for some time
Then my pojo should still have 2我有一个扩展JUnitStories的MyStories类,如下所示
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类,它们将共享相同的模型实例。
发布于 2020-01-08 14:04:58
在阅读了更多的内容并尝试了Jbehave with spring之后,1.我认为没有办法为每个线程创建不同的stepdef实例。2.在Jbehave中,没有等同于cucumber的world对象的threadsafe。
正如@VaL在问题的评论中提到的,我们必须显式地使threadsafe。
这是我到目前为止的理解。如果有更好的办法--请让我知道。
https://stackoverflow.com/questions/59567969
复制相似问题