citrusframework / citrusframework/citrus

Autowire test behaviors

Open
#617 1 comment 0 reactions 0 assignees View on GitHub
enhancement prio-medium
Dominant language
Java
Stars
485
Forks
155
Avg merge
4d 22h
Merged PRs (30d)
6

Description

**User story**
>As a developer, I want test behaviors to be autowired so I can easily use Spring beans in my test behaviors.

**Additional context**
>As the [documentation about test behavior](https://citrusframework.org/citrus/reference/2.8.0/html/index.html#test-behavior) states behaviors are reusable test action templates. Therefore I am creating test behaviors based upon, in my case, the `com.consol.citrus.dsl.runner.AbstractTestBehavior` and populate them with groups of actions I would like to reuse in various tests. Some of these actions require me to pass them instances which I have setup as beans managed by Spring. One can access these beans through the Spring application context which is available via `AbstractTestBehavior`. Therefore I need to call some overload of the `getBean` method via `org.springframework.context.ApplicationContext`. But there are several disadvantages with this approach.

>For starters it is very tedious to call this method every time I need access to some bean. I could of course solve this by introducing some methods which do the bean resolving for me, maybe even centralize this in some utility class which are prone to be overly large, not cohesive, and therefore simply not feasible from a maintenance perspective.

>Another aproach would be to move the code away from the test behaviors to Spring beans which are managed and therefore subject to Spring dependency injection. But this is also not favorable, because of there nature, Spring beans should be kept stateless, while test behaviors are intended to be statefull. The Citrus [sample project involving behaviors](https://citrusframework.org/samples/behavior/) depicts this clearly by constructing a test behavior using the Builder design pattern. So should I be moving the code away from the test behaviors I will need to have somehow access to the current ``TestRunner`` inside my Spring beans and also introduce ways to conserve and pass allong other statefull data to and between beans. The involves yet other layers of complexity and code to maintain which I would rather stay away from, ever so since Citrus already facilitates creating action templates with test behavior.

>Because of the named issues with the current interaction between test behaviors and beans I suggest test behaviors to be susceptible to autowiring by Spring. This concept is also in play when [Citrus is combined with Cucumber](https://citrusframework.org/citrus/reference/2.8.0/html/index.html#cucumber-spring), where Spring beans can be autowired in Cucumber step definition classes. The `DefaultTestRunner` already sets the Spring `ApplicationContext` on an applied behavior (see implementation `applyBehavior` method), so why not call `applicationContext.getAutowireCapableBeanFactory().autowireBean(behavior)` before executing the behavior?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.