Powermockito 私有方法模拟 NullPointerException。调用私有方法

nbr*_*est 5 java junit unit-testing mocking powermockito

我正在尝试模拟私有方法 (executeGetRequest),并且在我声明要为私有方法返回的模拟行中,私有方法实际上是使用空参数执行的,因此抛出 NullPointerException。

VlcPlayerMinimal.java:

package com.nicobrest.kamehouse.vlcrc.model;

public class VlcPlayerMinimal {

  public static void main(String[] args) {
    String vlcRcStatus = new VlcPlayerMinimal().getVlcRcStatus();
    System.out.println(vlcRcStatus);
  }

  public String getVlcRcStatus() {
    Client client = new Client();
    GetRequest getRequest = new GetRequest();
    String vlcRcStatus = executeGetRequest(client, getRequest);
    return vlcRcStatus;
  }

  private String executeGetRequest(Client client, GetRequest getRequest) {
    return client.execute(getRequest);
  }

  private class Client {
    public String execute(GetRequest getRequest) {
      return "{status: playing, id: 1}";
    }
  }
  private class GetRequest { 
  }
}
Run Code Online (Sandbox Code Playgroud)

VlcPlayerMinimalTest.java:

package com.nicobrest.kamehouse.model;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;

import com.nicobrest.kamehouse.vlcrc.model.VlcPlayerMinimal;

import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;

public class VlcPlayerMinimalTest {

  @Test
  public void getVlcRcStatusTest() {
    VlcPlayerMinimal vlcPlayerSpy = PowerMockito.spy(new VlcPlayerMinimal());
    try {
      PowerMockito.doReturn("{status: stopped, id: 2}").when(vlcPlayerSpy, "executeGetRequest", any(), any());
      String vlcRcStatus = vlcPlayerSpy.getVlcRcStatus();
      System.out.println(vlcRcStatus);
    } catch (Exception e) {
      e.printStackTrace();
      fail("Unexpected exception thrown.");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

java.lang.NullPointerException
    at com.nicobrest.kamehouse.vlcrc.model.VlcPlayerMinimal.executeGetRequest(VlcPlayerMinimal.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1846)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:810)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:675)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:95)
    at com.nicobrest.kamehouse.model.VlcPlayerMinimalTest.getVlcRcStatusTest(VlcPlayerMinimalTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Run Code Online (Sandbox Code Playgroud)

看来 PowerMockito 实际上是在调用我正在尝试模拟的方法 PowerMockito.doReturn("{status:stopped, id:2}").when(vlcPlayerSpy, "executeGetRequest", any(), any() );

它抛出异常,因为客户端为空,所以它在 null 上调用 execute(getClient),但这是我试图避免在测试中调用的方法。

任何想法如何解决这一问题?我已经尝试了一段时间没有成功。

我正在使用 Java 8、powermock 1.7.3 和 junit 4.12

jav*_*e42 4

本次测试成功:

package foo.bar;

import static org.junit.Assert.*;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest(VlcPlayerMinimal.class)
public class VlcPlayerMinimalTest {

  @Test
  public void getVlcRcStatusTest() {
    VlcPlayerMinimal vlcPlayerSpy = PowerMockito.spy(new VlcPlayerMinimal());
    try {
      PowerMockito.doReturn("{status: stopped, id: 2}").when(vlcPlayerSpy, "executeGetRequest", Mockito.any(), Mockito.any());
      String vlcRcStatus = vlcPlayerSpy.getVlcRcStatus();
      System.out.println(vlcRcStatus);
    } catch (Exception e) {
      e.printStackTrace();
      fail("Unexpected exception thrown.");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您需要这些类级别注释:

@RunWith(PowerMockRunner.class)
@PrepareForTest(VlcPlayerMinimal.class)
Run Code Online (Sandbox Code Playgroud)

控制台输出:

{状态:已停止,ID:2}

在此输入图像描述