无法使用PowerMock来模拟final类。

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

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'

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:

确定