This can be dangerous if the test modifies the content of the injected instance, as this change will be visible through all other injected blue instances, thus violating the expectations in the other (concurrently executed) tests or later in the same test.
Suggested solution:
Before injection, create a deep copy of the "etalon instance", and inject that copy instead.
Once the objective achieved, this should pass (pseudocode):
Deep copy is best for the test isolation but comes at performance price. The latter should be profiled, and if found insignificant then the default should be set to deep copy.
A way of configuration must be provided to control this behavior. For example, class-level annotation @RedAndBlueSettings with field createCopies would work nicely.
Create independent copies for injection
Curerntly, the same instance is injected at all injection points. I.e., this fails:
This can be dangerous if the test modifies the content of the injected instance, as this change will be visible through all other injected blue instances, thus violating the expectations in the other (concurrently executed) tests or later in the same test.
Suggested solution:
Before injection, create a deep copy of the "etalon instance", and inject that copy instead.
Once the objective achieved, this should pass (pseudocode):
Default - deep or shallow?
Deep copy is best for the test isolation but comes at performance price. The latter should be profiled, and if found insignificant then the default should be set to deep copy.
A way of configuration must be provided to control this behavior. For example, class-level annotation
@RedAndBlueSettings
with fieldcreateCopies
would work nicely.