Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ross-paypay opened this issue Sep 18, 2023 · 3 comments
Open

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

ross-paypay opened this issue Sep 18, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@ross-paypay
Copy link

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.

@Philzen
Copy link
Contributor

Philzen commented Oct 24, 2023

Confirming this issue on Java+JUnit5. Thanks @ross-paypay for pointing out the workaround.

Another workaround is to use injected field, which unfortunately reports that it is deprecated for removal in v5, so that is not a stable way forward.

@jackmatt2 jackmatt2 added the enhancement New feature or request label Jun 1, 2024
@cponfick
Copy link

It seems like the issue is not restricted to Kotlin. I have the same problem using java.

@jackmatt2
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants