英文:
can not mock final class using power mock
问题
我正在尝试使用 PowerMock 来模拟最终类(final class),但是得到了以下错误信息:"无法模拟/监视类 com.microsoft.azure.storage.blob.CloudBlockBlob\nMockito 无法模拟/监视,因为 :\n - 最终类",以下是我正在尝试的代码:
@RunWith(PowerMockRunner.class)
@PrepareForTest({CloudBlockBlob.class})
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
public class Test {
@org.junit.jupiter.api.Test
public void test() throws IOException, StorageException {
CloudBlockBlob cloudBlockBlob = PowerMockito.mock(CloudBlockBlob.class);
when(cloudBlockBlob.downloadText()).thenReturn("hello");
}
}
以及 Gradle 依赖如下:
testCompile group: 'junit', name: 'junit', version: '4.13'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
英文:
I'm trying to mock the final class using power mock but getting "Cannot mock/spy class com.microsoft.azure.storage.blob.CloudBlockBlob\nMockito cannot mock/spy because :\n - final class" below is the code which i am trying
@RunWith(PowerMockRunner.class)
@PrepareForTest({CloudBlockBlob.class})
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
public class Test {
@org.junit.jupiter.api.Test
public void test() throws IOException, StorageException {
CloudBlockBlob cloudBlockBlob = PowerMockito.mock(CloudBlockBlob.class);
when(cloudBlockBlob.downloadText()).thenReturn("hello");
}
}
and the gradle dependencies are as below:
testCompile group: 'junit', name: 'junit', version: '4.13'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
专注分享java语言的经验与见解,让所有开发者获益!
评论