2016-12-05 101 views
6

我正在從我正在進行的項目中刪除Powermock,所以我試圖用Mockito(mockito-core-2.2.28)重寫一些現有的單一測試。模擬Mockito 2的最終課程

當我運行測試,我有以下錯誤:

org.mockito.exceptions.base.MockitoException:

Cannot mock/spy class com.ExternalpackagePath.Externalclass

Mockito cannot mock/spy because :

  • final class

我知道這個問題已經被問(How to mock a final class with mockitoMock objects calling final classes static methods with Mockito),但我沒有找到我要找的答案對於。

這裏是我的代碼的摘錄:

public class MyClassToTest extends TestCase { 
    private MyClass myClass; 
    @Mock private Externalclass ext; // This class is final, I would like to mock it 

    @Override 
    protected void setUp() throws Exception { 
     MockitoAnnotations.initMocks(this); // <<<< The exception is thrown here 
     ext = Mockito.mock(Externalclass.class); 
    } 
} 

作爲文檔的Mockito中提到(https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2,§Mock的unmockable),我增加了org.mockito.plugins.MockMaker文件。這是我的項目的樹:

  • 項目
    • SRC
      • com.packagePath.myPackage
        • myClass的
    • 測試 個
      • com.packagePath.myPackage
        • myClassToTest
      • 資源
        • 的Mockito的擴展
          • org.mockito.plugins.MockMaker

我還試圖把「資源」目錄中的「SRC」,在一個名爲「測試」子目錄,但結果還是一樣。

我以爲Mockito v2可以嘲笑最後的結局。有人知道這裏缺少什麼嗎?

謝謝!

+0

該文件必須命名爲org.mockito.plugins.MockMaker,而不是org.mockito.plugins。 –

+0

Woops,對不起,我在複製/過去時忘記了行結束,文件的名稱寫在我的案例中。 我的不好!我編輯了文字說明 – Ptiseb

回答

7

嗯,我發現這裏有什麼問題,這可能對其他人有用。我的項目樹是錯誤的,我把org.mockito.plugins.MockMaker直接放在「src」的目錄「mockito-extension」中。這是現在我的樹:

    • SRC
      • com.packagePath.myPackage
        • myClass的
      • 的Mockito的擴展
        • org.mockito.plugins.MockMaker
  • 測試
    • com.packagePath.myPackage
      • myClassToTest
+1

您不應在您的「主要」源集中包含「MockMaker」。 –

1

您似乎有類路徑問題,就像我一樣。

你以前的設置會還努力,但似乎

project/test/resources 

在classpath沒有。

我試圖用IntelliJ運行這個時遇到了同樣的問題。我只是將資源目錄標記爲測試資源根目錄,它工作正常。讚美Mockito的神!

+0

這是爲我做的! – breezymri

13

奇怪你的解決方案似乎工作。
根據他們的文件Github它說。

Mocking of final classes and methods is an incubating, opt-in feature. It uses a combination of Java agent instrumentation and subclassing in order to enable mockability of these types. As this works differently to our current mechanism and this one has different limitations and as we want to gather experience and user feedback, this feature had to be explicitly activated to be available ; it can be done via the mockito extension mechanism by creating the file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker containing a single line:

mock-maker-inline 

After you created this file, Mockito will automatically use this new engine and one can do :

final class FinalClass { 
    final String finalMethod() { return "something"; } 
} 

FinalClass concrete = new FinalClass(); 

FinalClass mock = mock(FinalClass.class); 
given(mock.finalMethod()).willReturn("not anymore"); 

assertThat(mock.finalMethod()).isNotEqualTo(concrete.finalMethod()); 

In subsequent milestones, the team will bring a programmatic way of using this feature. We will identify and provide support for all unmockable scenarios. Stay tuned and please let us know what you think of this feature!

我的工作結構現在看起來像這樣。

enter image description here

7

我無法得到它的配置文件或者工作;然而,Mockito團隊非常友好,並且還提供了預配置的Mockito工件,無需在目標項目中進行配置。

所以,如果你使用的搖籃,想測試你的科特林代碼,只需添加到您的項目的依賴:在 而不是

testCompile "org.mockito:mockito-android:2.9.0" 

testCompile 'org.mockito:mockito-inline:2.8.9' 
testCompile('com.nhaarman:mockito-kotlin:1.5.0') { 
    exclude group: 'org.jetbrains.kotlin' 
    exclude group: 'org.mockito' 
} 
+0

謝謝你發佈這個。 –

+0

排除組在這種情況下做了什麼? – rozina

+0

它確保您在此處引用的內聯版本不會被此處繪製的'mockito-kotlin'的傳遞依賴關係覆蓋。 –

0

該解決方案爲我gradle文件,將其替換爲

testCompile group: 'org.mockito', name: 'mockito-inline', version: '2.9.0' 

它會工作。