测试私有静态方法,该方法在Java内部使用另一个私有静态方法

Nis*_*kar 3 java junit powermockito

我有一个A类,看起来如下:

public class A {

  // there is a public API that uses both
  // method1 and method2, but that's not important
  // in this question.
  public Integer publicApi(Integer a, Integer b) {
    // a bunch of other stuff is done that is 
    // not related to method1 or method2 here.
    // However, method1 IS used somewhere in here.
    // For now, I don't want to test other
    // implementation details for this API, I only
    // am interested in testing method1.

    return method1(a, b);
  }

  private static Integer method1(Integer a, Integer b) {
    if(method2(a, b, 10) == null) {
      return -1;
    }
    return 10 + method2(a, b, 10);
  }

  private static Integer method2(Integer a, Integer b, Integer c) {
    // some complicated logic with multiple values 
    // to return (based on various conditions).

    // however, I want to simply test what happens
    // when this method returns null (which is a possibility).
    return a+b+c;
  }
}
Run Code Online (Sandbox Code Playgroud)

如上所述,我的A类有一个公共API,它是(某种)复杂的逻辑,我不想测试(出于这个问题的目的).说我只对测试感兴趣method1.

这是我的测试代码(我正在使用PowerMockito):

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.lang.reflect.Method;
import static org.junit.Assert.assertNull;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;

@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class SimpleTest {

  @Test
  public void testMethod1Invocation() throws Exception {

    spy(A.class);

    when(A.class, method(A.class,
        "method2", Integer.class,
        Integer.class, Integer.class))
        .withArguments(1, 2, 10)
        .thenReturn(null);

    // use reflection to call method1
    Method method1 = A.class.getDeclaredMethod(
        "method1",
        Integer.class, Integer.class
    );

    method1.setAccessible(true);
    Object ret = method1.invoke(1, 2);
    assertEquals(-1, ret);
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

com.apple.geo.tart.A.method1(java.lang.Integer, java.lang.Integer, java.lang.Integer)
java.lang.NoSuchMethodException: com.apple.geo.tart.A.method1(java.lang.Integer, java.lang.Integer, java.lang.Integer)
    at java.lang.Class.getDeclaredMethod(Class.java:2130)
    at com.apple.geo.tart.SimpleTest.testMethod1Invocation(SimpleTest.java:32)
Run Code Online (Sandbox Code Playgroud)

我不知道我在这里做错了什么.在我看来,我需要三件事:

  1. 间谍,A以便我可以模拟/然后返回静态方法.
  2. 模拟我感兴趣的私有静态方法(method2在本例中).
  3. 使用反射访问method1,然后调用它.

然后我如何模拟私有静态方法(method2),以便我可以测试依赖于第一个的同一个类的另一个私有静态方法(method1)?

Mak*_*oto 5

我们讨论它的可测试性之前,我对这段代码有一些问题:

  • method2永远不会返回null实际上.如果它的任何输入是null,您将得到一个NullPointerException由于Java尝试取消装箱null.因此,您尝试测试的条件毫无意义.
  • 隐藏这些方法真的没有价值.它们似乎进行业务级计算,因此具有测试价值.

我会为你的初始案例说这么多:我没有得到你所做的同样的错误.从我在Java 8中看到的,您缺少一个invoke方法调用的参数,您需要传入method1该参数.

Object ret = method1.invoke(method1, 1, 2);
Run Code Online (Sandbox Code Playgroud)

但是,我根本不推荐这个. 让我们把它分成几个测试.

首先,让我们放松对这些方法的限制.他们可能是私人的,因为我们不会在其他地方暴露他们的行为,但如果我们将其削弱到包装私有,那么可能没有任何伤害.private从这些方法中删除标记.

其次,实际测试那些方法! 这些方法可能会受到a的影响null,您应该针对该方案进行测试.

第三,只有你测试了其他两种方法后,你才能回到你的publicApi方法.此时,您关心的只是使用正确的参数调用该方法.