编程语言
首页 > 编程语言> > java-Powermockito私有方法模拟NullPointerException.调用私有方法

java-Powermockito私有方法模拟NullPointerException.调用私有方法

作者:互联网

我正在尝试模拟私有方法(executeGetRequest),并且在声明要为私有方法返回的模拟的那一行中,私有方法实际上是使用null参数执行的,因此抛出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 { 
  }
}

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.");
    }
  }
}

堆栈跟踪:

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)
...

看来PowerMockito实际上在PowerMockito.doReturn(“ {status:stopd,id:2}”)行中调用了我要模拟的方法.when(vlcPlayerSpy,“ executeGetRequest”,any(),any() );

而且它抛出异常是因为client为null,所以它在null上调用execute(getClient),但这是我试图避免在测试中调用的方法.

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

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

解决方法:

此测试成功:

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.");
    }
  }
}

您需要以下类级别注释:

@RunWith(PowerMockRunner.class)
@PrepareForTest(VlcPlayerMinimal.class)

控制台输出:

{status: stopped, id: 2}

enter image description here

标签:powermockito,unit-testing,mocking,junit,java
来源: https://codeday.me/bug/20191025/1929447.html