2017-08-10 18 views
0

的我寫一個單元測試的一類,並使用無法使用PowerMockRunner因爲AbstractCommonPowerMockRunner未找到錯誤

@RunWith(PowerMockRunner.class). 

我得到不兼容的類型錯誤說

「發現org.powermock.modules。 junit4.PowerMockRunner但需要org.junit.runner.Runner。「

搖籃依賴關係:

testCompile 'org.mockito:mockito-core:2.1.0' 
testCompile 'org.powermock:powermock:1.6.1' 
testCompile 'org.powermock:powermock-core:1.6.1' 
testCompile 'org.powermock:powermock-module-junit4:1.6.2' 
testCompile 'org.powermock:powermock-api-mockito:1.6.2' 
+0

對於記錄:你確定你需要PowerMock嗎?它只有*合法*使用它的理由:當你測試第三方代碼時,你必須在那裏模擬新的/靜態的調用。當你編寫你自己的產品代碼時,**好**設計可以單獨使用Mockito進行測試(請注意,PowerMock迫使你使用已有幾年的Mockito--你錯過了Mockito 2最新版本中的所有優點) ! – GhostCat

+0

我想檢查代碼是否在不同的場景下執行try-catch塊。所有的catch塊都有LOGGER語句,LoggerFactory在我的類中用作靜態對象。因此,選擇PowerMock來檢查日誌語句是否被執行。 –

回答

0

你應該添加到您的build.gradle

testCompile 'junit:junit:4.12' 
testCompile 'org.mockito:mockito-all:1.10.19' 
testCompile 'org.powermock:powermock-module-junit4:1.6.6' 
testCompile 'org.powermock:powermock-api-mockito:1.6.6' 

版本在這裏非常重要,因爲PowerMock不支持2+的Mockito直到1.7.0

+0

我試過這種組合,仍然遇到錯誤 –

+0

,這清楚地表明依賴性問題。檢查你的類路徑中是否有不相關的依賴項。運行'gradle clean build --refresh-dependencies',如果它不幫助嘗試'gradle dependencies'來分析依賴關係。檢查下面的代碼段對我有用:https://gist.github.com/opensso/c34cb1d56d85fbaf6c0f3220c85070f7 –

+0

我有其他mockito依賴衝突,這種組合的作品。 –

相關問題