You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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?
Error shows the following >>>>
The text was updated successfully, but these errors were encountered: