I'm experiencing strange error while changing mockito-core
version. My code:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import static org.mockito.Mockito.validateMockitoUsage;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
@RunWith(PowerMockRunner.class)
@PrepareForTest(value = App.class)
public class TestClass {
@Before
public void setup() {
mockStatic(App.class);
when(App.getInstance()).thenReturn(mock(App.class));
}
// tests
}
Mocking App.getInstance()
works fine when i'm using
testImplementation group:'org.mockito',name:'mockito-core',version:'2.23.0'
But changing that to newest version
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.28.2'
Gives me error
org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class
On line when(App.getInstance()).thenReturn(mock(App.class));
My build.gradle mockito part:
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.28.2'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.2'
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.2'
Could anyone provide any ideas why changing mockito-core
is causing this issue?
Could anyone provide any ideas why changing mockito-core is causing this issue?
Sure: PowerMock(ito) doesn't just work with any version of Mockito.
See their documentation:
Mockito PowerMock
2.8.9+ 2.x
See that. 2.8.something. Not 2.28.something
You are simply lucky that 2.23 works, as that version is also not supported.
Another good reason to be really cautious about buying into the PowerMock dependency: it significantly reduces your ability to use newer versions of Mockito.
Thus my personal two cent: the only reasonable use case for PowerMock(ito) is when you have legacy code that can't be tested otherwise. For any other project, especially when writing your own, new code: simply use standalone Mockito. And instead of using the PowerMock hammer to test hard-to-test code, spend some time to learn how to write easy-to-test code.
Long story short: when using PowerMock, you better stick to versions of Mockito that have the "official" approval by Team PowerMock.
If you don't like to be restricted in such ways, I think I told you how to get out of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With