2

我正在尝试使用 powermock 来模拟私有方法,但我的 PowerMock 在MockitoBusinessOperation MockitoBusinessOperation = PowerMock.createPartialMock(MockitoBusinessOperation.class, "inTestMethod");. 我使用了 maven,mockito 和 powermock 的依赖项在我的 pom 文件中定义

<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);  
}

有人有想法或任何线索导致解决方案

4

2 回答 2

0

根据文档,您的 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>
于 2013-01-18T01:37:16.983 回答
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);
        }
于 2017-09-27T09:58:34.797 回答