簡體   English   中英

嘗試驗證 static 方法時出現 NotAMockException

[英]NotAMockException when trying to verify a static method

我正在嘗試驗證在單元測試中的特定配置中未調用 static 方法。

因此,我正在使用 PowerMock ( powermock-core:2.0.4 & powermock-module-junit4:2.0.4 ) 及其 Mockito API ( powermock-api-mockito2:2.0.4 )。

做的時候

PowerMockito.mockStatic(MyClass.class);

serviceUnderTest.methodThatShouldNotCallStaticMethod(arg1, arg2); //service not of type MyClass of course

PowerMockito.verifyStatic(MyClass.class, never());
MyClass.staticMethod(any(), any());

關於 class 中的測試方法,注釋為

@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class})

我收到以下錯誤: org.mockito.exceptions.misusing.NotAMockException: Argument passed to verify() is of type Class and is not a mock! .

我做錯了什么以及如何解決?

謝謝

原來是一個Powermock錯誤......

這是任何可能感興趣的人的解決方法

  1. 在 gradle 中刪除行testImplementation 'org.mockito:mockito-inline:2.13.0 "
  2. 創建由單行mockito.mock-maker-class=mock-maker-inline組成的src\test\resources\org\powermock\extensions\configuration.properties文件

我沒有 mockito-inline,我有 mock-maker.inline,我的設置是 org.powermock:powermock-api-mockito2:2.0.9 和 org.mockito:mockito-core:3.10.0。

However I found that PowerMockito.verifyStatic is using Mockito.verify underneath and wheh the class is checked for isMock mockito uses wrong MockMaker and can not find the class in mockedStatics collection.

當我使用

Mockito.mockStatic(MyClass.class); 

反而

PowerMockito.mockStatic(MyClass.class);

它開始對我很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM