使用Powermock模拟静态方法调用

时间:2018-01-22 17:43:47

标签: java unit-testing mocking mockito powermock

我需要为Utilities.java中的方法编写一个单元测试用例,其中包含

  client = ABCClient.Factory.newInstance(connectionProperties);

以下是我在测试用例中所做的事情

@PrepareForTest({ABCClient.class,ABCClient.Factory.class})
@Test
public void testGetABCClient() throws Exception
{
  PowerMockito.mockStatic(ABCClient.class);
  PowerMockito.mockStatic(ABCClient.Factory.class);

  PowerMockito.when(ABCClient.Factory.newInstance(any(Properties.class)))
      .thenThrow(new RuntimeException());
  try
  {
    Properties p = new Properties();
    idpsUtilities.getABCClient(p);
  }
  catch (Exception ex)
  {
    throw ex;
  }
}

我收到此错误

  

org.powermock.api.mockito.ClassNotPreparedException:

     

[Ljava.lang.Object; @ 448ff1a8 ABCClient类没有准备好   测试

我做错了什么?

这是我的pom

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
<spring-boot.version>1.5.9.RELEASE</spring-boot.version>
<packaging.type>jar</packaging.type>
<hystrix.version>1.4.0.RELEASE</hystrix.version>
<powermock.version>1.7.1</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-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.5</version>
<scope>test</scope>
</dependency>
</dependencies>

0 个答案:

没有答案