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

anyChar() ArgumentMatchers not working for Mockito.mockStatic #2792

Closed
tajuddinkhandaker opened this issue Nov 16, 2022 · 1 comment
Closed

Comments

@tajuddinkhandaker
Copy link

tajuddinkhandaker commented Nov 16, 2022

I have one class where one public non-static method executes a static method call inside requires to mock static when I tried to test in junit with mockito.

What did i do wrong?

class DummyClass {

    public boolean filter(CharSequence source) {
        return Character.isHighSurrogate(source.charAt(7));
    }
}

@Test
public void testDummyCharacterMockedStatic() {
   try (MockedStatic<Character> mocked = Mockito.mockedStatic(Character.class)) {
       CharSequence source = "안녕하세요 세계";
       mocked.when(() -> Character.isHighSurrogate(anyChar())).thenReturn(true);
       DummyClass d = new DummyClass();
       assertTrue(d.filter(source));
   }
}

Error shows the following >>>>

Misplaced or misused argument matcher detected here:

-> at DummyTest.lambda$testDummyCharacterMockedStatic$0(DummyTest.java:37)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last
matcher is returning an object like any() but the stubbed method
signature expect a primitive argument, in this case, use primitive
alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with
methods that cannot be mocked. Following methods cannot be
stubbed/verified: final/private/equals()/hashCode(). Mocking methods
declared on non-public parent classes is not supported.

@TimvdLippe
Copy link
Contributor

Unfortunately, Character is one of the classes that Mockito relies on internally for its behavior. Stubbing Character will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on a DoNotMock feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as "Infeasible". Apologies for the uninformative exception that is thrown.

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