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

Support for @Mock fields in JUnit5's @Nested classes #716

Open
sinhote opened this issue Sep 23, 2021 · 0 comments
Open

Support for @Mock fields in JUnit5's @Nested classes #716

sinhote opened this issue Sep 23, 2021 · 0 comments

Comments

@sinhote
Copy link

sinhote commented Sep 23, 2021

  • Version of JMockit that was used: 1.41

  • Description of the problem or enhancement request:

I've seen that if one uses a @nested class containing fields annotated with @mocked, those are not initialized (resulting in NPEs).

An obvious workaround is to move those fields to the outer class, but this somewhat goes against the spirit of nested classes, which allow to encapsulate related groups of tests. If the mocked fields are only relevant to the nested class, why should they be defined outside?

I've seen this was already reported in #458, but it was closed by simply saying "only fields in outer classes are supported by @mock". Can this be reconsidered?

Here's a sample code demonstrating the issue:

class MockedNestedTest {
    @Nested
    class Field {
        @Mocked Foo foo;
        @Test
        void shouldInitializeField() {
            Assertions.assertNotNull(foo); // this fails
        }
    }

Thanks!

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

No branches or pull requests

1 participant