Skip to content

Kotlin+Junit 5 Expect is not injected when using @Nested tests #158

Description

@ross-paypay

When using nested tests, Expect is not injected into the outer class.

Example

@ExtendWith(SnapshotExtension::class)
class ExampleTest {

    lateinit var expect: Expect

    @Test
    fun foo() {
        expect.toMatchSnapshot("foo")
    }

    @Nested
    inner class MyNestedTest {
        @Test
        fun bar() {
            expect.toMatchSnapshot("bar") // Nullpointer `expect` is null
        }
    }
}

This can be worked around by defining expect in the inner class

Work around example
@ExtendWith(SnapshotExtension::class)
class ExampleTest {

    lateinit var expect: Expect

    @Test
    fun foo() {
        expect.toMatchSnapshot("foo")
    }

    @Nested
    inner class MyNestedTest {
        lateinit var expect: Expect
        @Test
        fun bar() {
            expect.toMatchSnapshot("bar") // Nullpointer `expect` is null
        }
    }
}

Personally, I think it would be the most intiutive / least boilerplate to inject at the class with the @ExtendWith(SnapshotExtension.class) annotation. Or maybe if possible, walk the class tree and inject into all classes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions