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

Annotation-based spying on a generic class breaks existing final/inline Spies #3160

Closed
matej-staron opened this issue Oct 27, 2023 · 0 comments · Fixed by #3173
Closed

Annotation-based spying on a generic class breaks existing final/inline Spies #3160

matej-staron opened this issue Oct 27, 2023 · 0 comments · Fixed by #3173

Comments

@matej-staron
Copy link

Hello,

I encountered an issue with JUnit5/Mockito when using @Spy annotation with generic types.
Such configuration seems to break existing spies of Java record instances (inline mocks). The issue also occurs when using Mockito.spy() directly instead of the @Spy annotation. Example:

@ExtendWith(MockitoExtension.class)
class GenericSpyFailingTest {

  // Removing this spy makes the test pass.
  @Spy
  private final List<String> genericSpy = List.of("item A", "item B");

  @Spy
  private ExampleRecord exampleRecord = new ExampleRecord("some value");

  @Test
  void exampleServiceUsesDependency() {
    // The mocked record has all attributes set to null
    // despite being explicitly defined.
    assertNotNull(exampleRecord.someParameter());
  }
}

See the example repo with tests to reproduce.

Any idea why this happens? I couldn't find any mention of limitations related to using @Spy with generics.

This was originally encountered while using mockito-inline and an older Mockito version, but it is also reproducible with the latest mockito-core, as shown in the linked repo.

Any help is appreciated!

LeMikaelF pushed a commit to LeMikaelF/mockito that referenced this issue Nov 20, 2023
LeMikaelF pushed a commit to LeMikaelF/mockito that referenced this issue Nov 28, 2023
TimvdLippe pushed a commit that referenced this issue Nov 28, 2023
Fixes #3160

Co-authored-by: Mikaël Francoeur <mikael.francoeur@ticketmaster.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant