Reputation: 339
I'm new Jbehave. Im trying to find a way to achieve world in cucumber with Jbehave. I generated project with jbehave with pico archetype. Im trying to run two stories in parallel. I updated threads in pom.xml to 2. Consider a sample stepdef class as below
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);
}
}
I have Pojo model as below
public class Pojo {
private int i;
public void setI(int i){
this.i = i;
}
public int getI(){
return this.i;
}
}
My two stories are as below 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
I have MyStories class which extends JUnitStories as below
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;
}
}
When I don't run stories in parallel, they succeed. When I run stories in parallel, they are failing.
@@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 [email protected] 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 [email protected] 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>)
I'm not able to find documentation on how to run tests in parallel with Jbehave and pico. Also I need to have a model outside my stepdef class as I will be having multiple stefdef classes which will share the same model instance.
Upvotes: 0
Views: 182
Reputation: 339
After reading a bit more and trying out Jbehave with spring,
1. I think there is no way to get different instances of stepdef created per thread.
2. In Jbehave, there is no equivalent of cucumber's world
object which is threadsafe.
As @VaL mentioned in the comments of the questions, we have to make thinks threadsafe explicitly.
This is my understanding as of now. If there is any better way - kindly let me know.
Upvotes: 0