Skip to content

[JUnit 5] Add support for nested tests #24

Open
@grimsa

Description

@grimsa

Nested test docs: https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested

Currently snapshot matching within nested test classes is not supported.

Example:

class Demo {
    @BeforeAll
    static void beforeAll() {
        SnapshotMatcher.start();
    }

    @AfterAll
    static void afterAll() {
        SnapshotMatcher.validateSnapshots();
    }

    @Test
    void one() {
        expect("one").toMatchSnapshot();       // succeeds
    }

    @Nested
    class NestedTest {
        @Test
        void two() {
            expect("two").toMatchSnapshot();         // fails
        }

        @Nested
        class DeeplyNestedTest {
            @Test
            void three() {
                expect("three").toMatchSnapshot();       // fails
            }
        }
    }
}

Stack trace:

io.github.jsonSnapshot.SnapshotMatchException: Could not find method two on class class java.lang.Object
Please annotate your test method with @Test and make it without any parameters!

	at io.github.jsonSnapshot.SnapshotMatcher.lambda$getMethod$4(SnapshotMatcher.java:174)
	at java.util.Optional.orElseThrow(Optional.java:290)
	at io.github.jsonSnapshot.SnapshotMatcher.getMethod(SnapshotMatcher.java:172)
	at io.github.jsonSnapshot.SnapshotMatcher.lambda$getMethod$3(SnapshotMatcher.java:171)
	at java.util.Optional.map(Optional.java:215)
	at io.github.jsonSnapshot.SnapshotMatcher.getMethod(SnapshotMatcher.java:171)
	at io.github.jsonSnapshot.SnapshotMatcher.expect(SnapshotMatcher.java:99)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions