-
Notifications
You must be signed in to change notification settings - Fork 3
Test Case Scenario bean scope
Time to time in regular automation life next scenarios make happens. Inside of when step request to AUT happens and some result fetched. Then, depends on BDD style, one or several then steps should happen to validate that response from AUT is correct. To code this, local variable should be introduced to keep system response.
It looks simple but on practice test automation require multi threading execution. In this case local non thread safe variable can produce false fails when response from one thread validated by checks from other thread.
To solve this simple raw Java solutions exist like ThreadLocal
. This solution looks simple but again require some additional effort to correctly clean and setup thread local variable.
Sprimber provide to use full power from Spring based applications. In Spring everything is bean, and container is a kind of manager of beans. So, during runtime some beans can be removed or created from scratch, some of them can be created with different visibility depth. Spring container need to have a recipe of how to create and manage the beans and one of the parts of this recipe is a definition of scopes.
Bean scope allow to set visibility of this bean like singleton
when the same instance of object will be autowired everywhere or provided
when new instance of bean will be created per invocation.
From the box Sprimber shipped with Scenario Scope. This will allow to define the step context objects that will share the state only inside of one scenario. In other worlds steps inside of scenario will be able to share information but cannot access information from other scenarios.
To disable default Srpimber scope property sprimber.configuration.custom.scopes.enable
should be set to false.
There are two ways to specify scope for custom bean.
-
@ScenarioComponent
- component style. This is a composite annotation that mark the class to auto scan and also set the scope for this bean as a scenario. -
@ScenarioScope
- bean style. This is a more convenient way to mark some@Bean
methods with this annotation instead of raw@Scope
annotation from Spring.
@ScenarioComponent
public class StepContext {
private Integer value;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
To share information/context between steps in thread safe way new class should be created. This is a simple POJO like class where any type of fields will be declared. Sprimber guaranteed that one instance of class marked with Scenario scope will be created by each scenario, but there is no guarantee what is inside of this object, since it more project specific.
@ScenarioComponent
annotation on this class mark this class as available for classpath scanning and set scope to scenario.
During runtime for each new scenario new instance of StepContext
will be created. Depends on project needs one or several object with scope Scenario can be created - depends on domain logic.
@Actions
public class ScopeSteps {
private final StepContext stepContext;
public ScopeSteps(StepContext stepContext) {
this.stepContext = stepContext;
}
@Given("put to the step context value '{int}'")
public void givenValueInStepContext(Integer value) {
stepContext.setValue(value);
}
@Then("step context contains value '{int}'")
public void thenValueInStepContextIsCorrect(Integer value) {
Assertions.assertThat(stepContext.getValue()).as("Step context contains old value").isEqualTo(value);
}
}
Class above shows simple usage of shared context. Shared context is just one more simple bean that autowired as usually. Next inside of steps communication with object happens in the regular way. Sprimber will cover in runtime multi threading invocation to this object and provide new context per each new scenario.
Sprimber Scenario Scope provides ready solution for typical business problem in industry. It leverage Spring Scope model and provide one more scope out of the box. Also it is possible to create own custom scopes and use them in parallel or without Sprimber Scenario Scope implementation.