首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jbehave和pico (Jbehave Pico)并行执行

使用Jbehave和pico (Jbehave Pico)并行执行
EN

Stack Overflow用户
提问于 2020-01-03 02:04:35
回答 1查看 83关注 0票数 0

我是新来的。我正在试图找到一种方法来实现黄瓜与Jbehave的世界。我生成了带有jbehave和pico原型的项目。我正在尝试并行运行两个故事。我将pom.xml中的线程更新为2。

代码语言:javascript
复制
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模型

代码语言:javascript
复制
  public class Pojo {

    private int i;

    public void setI(int i){
        this.i = i;
    }

    public int getI(){
        return this.i;
    }
}

我的两个故事如下PojoOne.story

代码语言:javascript
复制
    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

代码语言:javascript
复制
 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类,如下所示

代码语言:javascript
复制
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;
    }
 }

当我不并行运行故事时,它们就会成功。当我并行运行故事时,它们是失败的。

代码语言:javascript
复制
 @@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类,它们将共享相同的模型实例。

EN

回答 1

Stack Overflow用户

发布于 2020-01-08 14:04:58

在阅读了更多的内容并尝试了Jbehave with spring之后,1.我认为没有办法为每个线程创建不同的stepdef实例。2.在Jbehave中,没有等同于cucumber的world对象的threadsafe。

正如@VaL在问题的评论中提到的,我们必须显式地使threadsafe。

这是我到目前为止的理解。如果有更好的办法--请让我知道。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59567969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档