2013-01-18 49 views
2

我想用powermock來模擬私有方法,但我的PowerMock在MockitoBusinessOperation MockitoBusinessOperation = PowerMock.createPartialMock(MockitoBusinessOperation.class, "inTestMethod");中未被識別。我用行家和和的Mockito的powermock依賴在我的POM文件如何使用Mockito和TestNG模擬使用PowerMock的私人方法

<dependency> 
    <groupId>org.mockito</groupId> 
    <artifactId>mockito-core</artifactId> 
    <version>1.8.5</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>org.powermock</groupId> 
    <artifactId>powermock-mockito-release-full</artifactId> 
    <version>1.4.9</version> 
    <scope>test</scope> 
</dependency> 

我不知道,如果錯誤與使用TestNG到powermock或我做我的代碼中一些錯誤的定義。

@PrepareForTest(MockitoBusinessOperation.class) 
@Test(enabled = true) 
public void testReCalculatePrepaids() throws Exception { 
    MockitoBusinessOperation MockitoBusinessOperation = PowerMock.createPartialMock(MockitoBusinessOperation.class, "inTestMethod"); 
    PowerMock.expectPrivate(MockitoBusinessOperation, "inTestMethod", Id).andReturn("working fine"); 

    when(MockitoBusinessService.creditReport(this.Id)).thenReturn(new String("Decline by only Me")); 

    String report = MockitoBusinessService.creditReport(this.Id); 
    String mainReport = MockitoBusinessOperation.creditAproved(this.Id); 
} 

有人有一個想法或任何線索導致該解決方案

+0

你應該真的考慮在你的編輯器中使用空格而不是製表符,這是w這種格式不會很好地複製。空白空間(1)中的空間量是通用的,而製表符不是。 –

+0

感謝您的格式修復 – jan

回答

0

按照documentation你的Maven文件應具有以下定義:

<properties> 
    <powermock.version>1.5</powermock.version> 
</properties> 
<dependencies> 
    <dependency> 
     <groupId>org.powermock</groupId> 
     <artifactId>powermock-module-testng</artifactId> 
     <version>${powermock.version}</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.powermock</groupId> 
     <artifactId>powermock-api-mockito</artifactId> 
     <version>${powermock.version}</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 
0

請嘗試這種方式

@Test 
     public void commandEndHandlerTest() throws Exception 
     { 
      Method retryClientDetail_privateMethod =yourclass.class.getDeclaredMethod("Your_function_name",null); 
      retryClientDetail_privateMethod.setAccessible(true); 
      retryClientDetail_privateMethod.invoke(yourclass.class, null); 
     } 
相關問題