2013-01-18 57 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); 
     } 
相关问题