+-
java-如何使用PowerMockito模拟私有静态方法?
This is the same question as found here.很遗憾,接受的答案对我不起作用.我有一个带有需要测试的私有方法的静态实用程序类.我发现当我模拟这样的方法时:

PowerMockito.spy(StaticUtil.class);
PowerMockito.when(StaticUtil.class, "getSomethingMethod", someObjectArray, someStringArray, aBoolean, someList).thenReturn(anotherList);

我收到一个空指针异常,因为实际上正在调用getSomethingMethod().调试时,我看到在运行要测试的方法时未调用它,但是在设置模拟时却正在运行.以这种格式创建模拟时,应该发生Based on this site,.

因此,我尝试以这种方式设置模拟:

PowerMockito.spy(StaticUtil.class);        
PowerMockito.doReturn(anotherList).when(StaticUtil.getSomethingMethod( someObjectArray, someStringArray, aBoolean, someList);

但是,我从Eclipse中收到一条错误消息,提示我需要将getSomethingMethod()的可见性更改为public.可以模拟私有方法的使用PowerMockito的一大好处不是吗?我需要模拟此私有静态方法(在安装过程中无需实际调用该方法).

最佳答案
您必须使用在 the answer you linked中指定的确切语法.该语法是doReturn(returnValue).when(Class,String,arguments);.您在此处提供的所有示例均未使用该示例.

这是一些扩展的解释.我汇集了一个示例测试框架来演示这一点:

尝试在此类上运行测试:

package org.test.stackoverflow;

import java.util.Collections;
import java.util.List;

public class StaticUtil {
  public static void Wrapper() {
    getSomethingMethod(null, null, false, Collections.<String>emptyList());
  }

  private static List<String> getSomethingMethod(Object[] obj,
      String[] str, boolean flag, List<String> aList){ 
    System.out.println("I happen!");
    return aList;
  }
}

如果方法本身被调用,我们将看到发生!如果不是,我们不会.

然后,我使用以下测试类:

package org.test.stackoverflow;

import java.util.List;

import org.junit.runner.RunWith;
import org.junit.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(org.test.stackoverflow.StaticUtil.class)
public class StaticUtilTest {
  Object[] someObjectArray;
  String[] someStringArray;
  boolean aBoolean;
  List<String> someList;
  List<String> anotherList;

  @Test
  public void testWhenClassStringMethod() throws Exception {
    System.out.println("Beginning Test when(Class klass, String method name).doReturn(result)");
    PowerMockito.spy(StaticUtil.class);
    PowerMockito.when(StaticUtil.class, "getSomethingMethod", someObjectArray, someStringArray, aBoolean, someList).thenReturn(anotherList);
    System.out.println("End Test when");
  }

  @Test
  public void testDoReturnActualMethod() throws Exception {
    PowerMockito.spy(StaticUtil.class);
    // This doesn't compile as you've correctly stated
//    PowerMockito.doReturn(anotherList).when(StaticUtil.getSomethingMethod(someObjectArray, someStringArray, aBoolean, someList);
  }

  @Test
  public void testDoReturnClassStringMethod() throws Exception {
    System.out.println("Beginning Test doReturn().when(Class klass, String method name");
    PowerMockito.spy(StaticUtil.class);
    PowerMockito.doReturn(anotherList).when(StaticUtil.class, "getSomethingMethod", someObjectArray, someStringArray, aBoolean, someList);
    System.out.println("End Test doReturn");
  }
}

因此,如果可以打印,那我们就使用了错误的语法.当我运行该程序时,我们得到:

Beginning Test when(Class klass, String method name).doReturn(result)
I happen!
End Test when
Beginning Test doReturn().when(Class klass, String method name)
End Test doReturn

因此,您必须在第三个测试中使用语法.

注意:本示例使用静态的空参数.显然,您应该将示例配置为适合您的应用程序正常使用Argument Matchers.

点击查看更多相关文章

转载注明原文:java-如何使用PowerMockito模拟私有静态方法? - 乐贴网