I am writing a test with mocking a static class, TreeCacheClient. The dependency on test is (build.sbt, play framework):
libraryDependencies ++= Seq (
"commons-lang" %"commons-lang" % "2.6",
"org.apache.commons" % "commons-math3" % "3.0",
"org.apache.commons" %"commons-lang3" % "3.5",
"org.postgresql" % "postgresql" % "9.4.1210",
"com.google.guava" % "guava" % "23.0",
"org.powermock" % "powermock-module-junit4" % "1.7.3" % Test,
"org.powermock" % "powermock-api-mockito2" % "1.7.3" % Test,
"com.typesafe.play.modules" %% "play-modules-redis" % "2.5.0"
)
@RunWith(PowerMockRunner.class)
@PrepareForTest({CacheClient.class})
public class TreeServiceImplTest {
@Mock
private Configuration mockConfig;
@Mock
private CacheApi mockCache;
@Mock
private JedisPool mockRedisTool;
@Mock
private SegTree mockSeqTree;
private SegTreeService segTreeService;
private static final String CACHE_KEY = "testkey";
private static final String CHANNEL = "testchannel";
private static final String BACKUP_FILE = "file.ser";
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
segTreeService = new SegTreeServiceImpl(mockConfig, mockCache, mockRedisTool);
}
@Test
public void testGetSegTree() throws Exception {
when(mockConfig.getString(CONF_SEG_TREE_CACHE_KEY)).thenReturn(CACHE_KEY);
when(mockConfig.getString(CONF_PUBLISHER_CHANNEL_NAME)).thenReturn(CHANNEL);
when(mockConfig.getString(CONF_SEG_TREE_BACKUP_LOCATION)).thenReturn(BACKUP_FILE);
when(mockCache.get(CACHE_KEY)).thenReturn(mockSeqTree);
doNothing().when(mockCache).set(CACHE_KEY, mockSeqTree);
mockStatic(TreeCacheClient.class);
when(TreeCacheClient.deserialize(BACKUP_FILE)).thenReturn(null);
SegTree tree = segTreeService.getSegTree();
assertEquals(tree, mockSeqTree);
verifyStatic(TreeCacheClient.class, Mockito.times(0));
}
}
I expect the test with no errors.
But I always get:
[error] Test *.service.TreeServiceImplTest.testSubscribe failed: java.lang.NoSuchMethodError: org.powermock.reflect.internal.WhiteboxImpl.getUnproxiedType(Ljava/lang/Class;)Lorg/powermock/reflect/internal/proxy/UnproxiedType;, took 0.001 sec [error] at org.powermock.core.MockInvocation.init(MockInvocation.java:31) [error] at org.powermock.core.MockInvocation.(MockInvocation.java:22) [error] at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:151) [error] at org.powermock.core.MockGateway.methodCall(MockGateway.java:134) [error] at *.service.TreeServiceImplTest.setUp(SegTreeServiceImplTest.java)
even tested with 1.7.1 or 1.7.0 powermock lib. I also tried with different version of mockito2 lib. Still having this error. Any idea?