无法使用PowerMock来模拟final类。

huangapple 未分类评论44阅读模式
英文:

can not mock final class using power mock

问题

  1. 我正在尝试使用 PowerMock 来模拟最终类final class),但是得到了以下错误信息"无法模拟/监视类 com.microsoft.azure.storage.blob.CloudBlockBlob\nMockito 无法模拟/监视,因为 :\n - 最终类"以下是我正在尝试的代码
  2. @RunWith(PowerMockRunner.class)
  3. @PrepareForTest({CloudBlockBlob.class})
  4. @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
  5. public class Test {
  6. @org.junit.jupiter.api.Test
  7. public void test() throws IOException, StorageException {
  8. CloudBlockBlob cloudBlockBlob = PowerMockito.mock(CloudBlockBlob.class);
  9. when(cloudBlockBlob.downloadText()).thenReturn("hello");
  10. }
  11. }
  12. 以及 Gradle 依赖如下
  13. testCompile group: 'junit', name: 'junit', version: '4.13'
  14. testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0'
  15. 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

  1. @RunWith(PowerMockRunner.class)
  2. @PrepareForTest({CloudBlockBlob.class})
  3. @PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
  4. public class Test {
  5. @org.junit.jupiter.api.Test
  6. public void test() throws IOException, StorageException {
  7. CloudBlockBlob cloudBlockBlob = PowerMockito.mock(CloudBlockBlob.class);
  8. when(cloudBlockBlob.downloadText()).thenReturn("hello");
  9. }
  10. }

and the gradle dependencies are as below:

  1. testCompile group: 'junit', name: 'junit', version: '4.13'
  2. testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0'
  3. testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'

huangapple
  • 本文由 发表于 2020年7月27日 15:28:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/63110582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定