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

Sporadic java.lang.AssertionError in MockUtil.getMockHandlerOrNull #2793

Closed
XN137 opened this issue Nov 21, 2022 · 3 comments
Closed

Sporadic java.lang.AssertionError in MockUtil.getMockHandlerOrNull #2793

XN137 opened this issue Nov 21, 2022 · 3 comments

Comments

@XN137
Copy link
Contributor

XN137 commented Nov 21, 2022

hello,

we are using mockito 4.8.1 on jdk 11 targeting java 8

recently one of our maven modules added a dependency to mockito-inline in order to mock final classes/methods (normally modules use normal mockito)

we started seeing sporadically failing tests around this:

java.lang.AssertionError
	at org.mockito.internal.util.MockUtil.getMockHandlerOrNull(MockUtil.java:160)
	at org.mockito.internal.util.MockUtil.getMockHandler(MockUtil.java:115)
	at org.mockito.internal.stubbing.answers.InvocationInfo.isVoid(InvocationInfo.java:94)
	at org.mockito.internal.stubbing.answers.Returns.validateFor(Returns.java:34)
	at org.mockito.internal.stubbing.InvocationContainerImpl.addAnswer(InvocationContainerImpl.java:65)
	at org.mockito.internal.stubbing.InvocationContainerImpl.addAnswer(InvocationContainerImpl.java:56)
	at org.mockito.internal.stubbing.OngoingStubbingImpl.thenAnswer(OngoingStubbingImpl.java:32)
	at org.mockito.internal.stubbing.BaseStubbing.thenReturn(BaseStubbing.java:34)
        (..)

also:

java.lang.AssertionError
	at org.mockito.internal.util.MockUtil.getMockHandlerOrNull(MockUtil.java:160)
	at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:147)
	at org.mockito.internal.stubbing.StubberImpl.when(StubberImpl.java:43)
        (..)

for the latter error the test code looks like this:

import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.compute.models.GalleryImageVersions;

(..)
  @Test
  public void testMethod() {
    AzureResourceManager mockArmClient = mock(AzureResourceManager.class);  // final class
    GalleryImageVersions mockGalleryImageVersions = mock(GalleryImageVersions.class); // interface
    doReturn(mockGalleryImageVersions).when(mockArmClient).galleryImageVersions();
    doReturn(null).when(mockGalleryImageVersions).listByGalleryImage(anyString(), anyString(), anyString()); <- assertion fails here
    (..)
  }

additional notes:

  • these tests are not failing every time and the number of failing tests varies (but all with assertion error in MockUtil.getMockHandlerOrNull)
  • in case it is relevant: it could be that the maven module depends on both <artifactId>mockito-inline</artifactId> and <artifactId>mockito-core</artifactId> but these tests are not using multiple classloaders or the like

are we doing anything wrong or is this a mockito bug?

the failing assertion has only recently been added in 4.8.0 for supporting the mockmaker settings:
https://github.com/mockito/mockito/blame/3faa00299a8c0793cf17ce072229f2e8ffea4579/src/main/java/org/mockito/internal/util/MockUtil.java#L160
any ideas when this can resolve to different mock makers?

@TimvdLippe
Copy link
Contributor

Thanks for filing the issue. Could you please provide us a minimal reproduction case that we can clone/download to debug the issue? Thanks in advance!

@XN137
Copy link
Contributor Author

XN137 commented Nov 25, 2022

the full code is from a large private codebase, so i tried to distill it down to a minimal public example however now the error does not occur anymore with the mentioned code... (was running the test in a loop in 4 shells).

do you have any remote idea when this assertion might be failing?
when does mockMakers.values() return more than 1 element?
could it be related to classloader shenanigans maybe (i.e. Thread.currentThread().getContextClassLoader()) ?

@TimvdLippe
Copy link
Contributor

Seems like this is a similar issue to #2823 In your large codebase, it is likely that you have clashing usages of mockmakers, triggering this assertion. Hopefully that additional information can help you further debug this issue.

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

No branches or pull requests

2 participants