neboskreb / red-and-blue

JUnit 5 extension for easy injection of Red and Blue objects
Apache License 2.0
1 stars 0 forks source link

Create independent copies for injection #19

Open neboskreb opened 9 months ago

neboskreb commented 9 months ago

Create independent copies for injection

Curerntly, the same instance is injected at all injection points. I.e., this fails:

@Test
private foo(@BlueInstance one, @BlueInstance another) {
    assertNotSame(one, another);
}

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):

@Test
private foo(@BlueInstance one, @BlueInstance another) {
    assertDeepNotSame(one, another);
    assertDeepReflectionEqueals(one, another);
}

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 field createCopies would work nicely.

neboskreb commented 9 months ago

A library for deep cloning is available at Maven Central