jenkinsci / jenkinsci/plugin-pom
[JENKINS-60130] can not use recommended versions of powermock & mockito
- Dominant language
- Java
- Stars
- 75
- Forks
- 81
- Avg merge
- 9m
- Merged PRs (30d)
- 18
Description
using the plugin-pom 3.51 as a base for your plugins.
if you use powermock and mockito at the recommended versions tests that use powermock blowup with org.mockito.exceptions.base.MockitoException: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class java.lang.SystemMockito cannot mock/spy because : - final class at ShowBadMockitoVersion.testWeGetHere(ShowBadMockitoVersion.java:21)
// code placeholder
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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;@RunWith(PowerMockRunner.class)
@PrepareForTest({ System.class })
public class ShowBadMockitoVersion {
@Test public void testWeGetHere() throws Exception {
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getProperty(eq("zzz"), any())).thenReturn("TEST");
assertEquals("TEST", System.getProperty("zzz", "bogus"));
}
}
Downgrading the mockito version locally to 2.23.0 and powermock dependencies to 2.0.2 fixes the issue.
---
Originally reported by
teilo, imported from: can not use recommended versions of powermock & mockito
Raw content of original issue
using the plugin-pom 3.51 as a base for your plugins.
if you use powermock and mockito at the recommended versions tests that use powermock blowup with org.mockito.exceptions.base.MockitoException: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class java.lang.SystemMockito cannot mock/spy because : - final class at ShowBadMockitoVersion.testWeGetHere(ShowBadMockitoVersion.java:21)
// code placeholder
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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;@RunWith(PowerMockRunner.class)
@PrepareForTest({ System.class })
public class ShowBadMockitoVersion {
@Test public void testWeGetHere() throws Exception {
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getProperty(eq("zzz"), any())).thenReturn("TEST");
assertEquals("TEST", System.getProperty("zzz", "bogus"));
}
}
Downgrading the mockito version locally to 2.23.0 and powermock dependencies to 2.0.2 fixes the issue.
Contributor guide
Research direction
Start by inspecting plugin-pom 3.51's managed Mockito and PowerMock dependency versions, then reproduce the failure with the provided ShowBadMockitoVersion example. Done means tests using PowerMock can mock System with the recommended versions, without requiring the reported local downgrades to Mockito 2.23.0 and PowerMock 2.0.2.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100