java-snapshot-testing
java-snapshot-testing copied to clipboard
Wrong naming with Junit Jupiter Parallel Execution and nested tests
When running with @Execution(ExecutionMode.CONCURRENT) on class level and @Nested test classes, it seems that all tests are named after the first nested test class that is run and then all tests are added to that snapshot file.
Expected behaviour is of course that the names are the same with and without parallel execution of junit 5 tests.
Minimal example:
package se.pervanovo.bjelin.bjelinservice.servicefactory;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.springframework.boot.test.context.SpringBootTest;
import au.com.origin.snapshots.Expect;
import au.com.origin.snapshots.junit5.SnapshotExtension;
@SpringBootTest
@ExtendWith(SnapshotExtension.class)
@Execution(ExecutionMode.CONCURRENT)
public class ConcurrentTests {
@Test
void one(Expect expect) {
expect.toMatchSnapshot("one");
}
@Nested
@DisplayName("nested")
public class NestedTests {
@Test
void two(Expect expect) {
expect.toMatchSnapshot("two");
}
}
}
Resulting snapshot name: ConcurrentTests$NestedTests.snap
Resulting snapshot content:
se.pervanovo.bjelin.bjelinservice.servicefactory.ConcurrentTests$NestedTests.one=[
"one"
]
se.pervanovo.bjelin.bjelinservice.servicefactory.ConcurrentTests$NestedTests.two=[
"two"
]
It seems that this might be a bug in the JUnit Jupiter API BeforeAllCallback. When I run the test by itself, it correctly returns the current class name calling context.getTestClass(). When I run all the tests in a class, the junit5 hooks are returning the Nested Class instead for some reason.
https://github.com/origin-energy/java-snapshot-testing/blob/cee4af4ba194f8a06b3988332bc12812624ea542/java-snapshot-testing-junit5/src/main/java/au/com/origin/snapshots/junit5/SnapshotExtension.java#L34
Furthermore: When you mix test methods in and outside @Nested classes, one .snap.debug file will not get deleted after test completion. In the example above, ConcurrentTests.snap.debug will remain inside snapshots. When you move @Test one(...) into a @Nested class, everything gets cleaned up properly.
It seems book-keeping of files created is buggy.