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

Testing private static method that internally uses another private static method in Java

我有一个 class 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;
  }
}

正如给出的那样,我的 class A 有一个 public 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);
  }
}

但是,我收到以下错误:

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)

我看不出我做错了什么。在我看来,这里我需要三样东西:

  1. 监视 A 以便我可以在静态上模拟 when/thenReturn 方法。
  2. 模拟我感兴趣的私有静态方法(在本例中为method2)。
  3. 使用反射访问method1,然后调用它。

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

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

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

对于您的初始案例,我会说这么多:我没有得到与您相同的错误。根据我对 Java 8 的观察,您缺少 invoke 方法调用的参数,您需要通过该参数传递 method1 in.

Object ret = method1.invoke(method1, 1, 2);

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

首先,让我们放宽对这些方法的限制。它们可能是私有的,因为我们不会在其他地方暴露它们的行为,但如果我们将其弱化为包私有,则可能不会有任何危害。从这些方法中删除 private 标记。

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

第三,只有 之后,您是否测试了其他两种方法,您是否应该返回到您的 publicApi 方法。此时,您所关心的只是使用正确的参数简单地调用了该方法。