使用 Mockito 和 PowerMock 验证传递给静态方法的参数



我正在尝试测试一个 void 方法,通过验证它在调用它时传递给静态方法的参数。静态方法负责保留这些参数。

class ProxyHandler {
  public void process(String str) {
    // parse the str and populate x, y, z
    PersistManager.proxy(x, y, z); 
  }
}

下面的 PersistManager 包含我尝试捕获它收到的参数的静态方法。

class PersistManager {
  public static void proxy(String x, String y, String z) {
    // persist the x, y, z
  }
}

最后是我的测试类,测试ProxyHandler:

@RunWith(PowerMockRunner.class)
@PrepareForTest(PersistManager.class)
public class TestProxyHandler() {
   private ProxyHandler handler;
   @Before
   public void setUp() {
     handler = new ProxyHandler();
   }
   @Test
   public void testProxy() {
     PowerMockito.mockStatic(PersistManager.class);
     ArgumentCaptor<String> arg1 = ArgumentCaptor.forClass(String.class);
     ArgumentCaptor<String> arg2 = ArgumentCaptor.forClass(String.class);
     ArgumentCaptor<String> arg3 = ArgumentCaptor.forClass(String.class);
     handler.process("a long string");
     PowerMockito.verifyStatic(PersistManager.class, times(1));
     PersistManager.proxy(arg1.capture(), arg2.capture(), arg3.capture());
     assertEquals("x", arg1.getValue());
   }
}

运行测试时,出现以下错误:

Wanted but not invoked PersistManager.proxy(
    null,
    null,
    null
);
Actually, there were zero interactions with this mock.

我还注意到,如果我将 times(1( 的值更改为任何其他值,验证静态仍然通过。我做错了什么?

抱歉这么晚才回答这个问题,但我只是偶然发现它,因为我正在使用 Powermockito 调查其他事情。我复制了您的情况如下(对不起,我不喜欢默认包(:

package de.scrum_master.stackoverflow.q49946922;
class PersistManager {
  public static void proxy(String x, String y, String z) {
    System.out.printf("Persisting %s, %s, %s%n", x, y, z);
  }
}
package de.scrum_master.stackoverflow.q49946922;
public class ProxyHandler {
  public void process(String str) {
    String[] params = str.split(" ");
    PersistManager.proxy(params[0], params[1], params[2]);
  }
}
package de.scrum_master.stackoverflow.q49946922;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
@RunWith(PowerMockRunner.class)
@PrepareForTest(PersistManager.class)
public class ProxyHandlerTest {
  private ProxyHandler handler;
  @Before
  public void setUp() {
    handler = new ProxyHandler();
  }
  @Test
  public void testProxy() {
    PowerMockito.mockStatic(PersistManager.class);
    ArgumentCaptor<String> arg1 = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> arg2 = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> arg3 = ArgumentCaptor.forClass(String.class);
    handler.process("a long string");
    PowerMockito.verifyStatic(PersistManager.class, times(1));
    PersistManager.proxy(arg1.capture(), arg2.capture(), arg3.capture());
    assertEquals("a", arg1.getValue());
    assertEquals("long", arg2.getValue());
    assertEquals("string", arg3.getValue());
  }
}

这完美无缺。您可能做错了什么,或者您未显示的代码中存在问题。无需切换到JMockit。

顺便说一句,我使用了PowerMock 2.0.2,Mockito 2.23.0,JUnit 4.12,Java 8。

相关内容

  • 没有找到相关文章

最新更新