模拟具有静态方法的 class 时发生异常

Exception occurred when mock a class which having static method

我正在为静态方法编写单元测试(使用 TestNG)。模拟静态方法的 class 时出现异常。

@RunWith(PowerMockRunner.class)
@PrepareForTest(TempClass.class)
public class MyTestClass {

  @Test
  public void testMethodt() {
    PowerMockito.mockStatic(TempClass.class);
  }
}

public class TempClass {

  public static String getName(String name){
    return "Hi " + name;
  }
}

当执行 PowerMockito.mockStatic(TempClass.class); 时出现以下异常

org.powermock.api.mockito.ClassNotPreparedException: 

[Ljava.lang.Object;@65466a6a
The class com.test.TempClass not prepared for test

有什么解决办法吗?

通过添加以下内容解决了问题

@PrepareForTest(TempClass.class)
public class MyTestClass extends PowerMockTestCase {
   ....
}