【问题标题】:How do I completely mock out a class with PowerMockito?如何使用 PowerMockito 完全模拟一个类?
【发布时间】:2015-02-23 20:35:35
【问题描述】:

我正在阅读这个documentation on PowerMockito,它有两个主要示例:

  1. 模拟静态方法
  2. 部分模拟类

但我想知道如何模拟使用new 创建的整个类。 我正在寻找 Mockito 的 mock 方法的 PowerMockito 版本。这应该能够以某种方式用 Mockito mock(Foo.class) 替换我的生产代码中的 new Foo()。这是我尝试过的:

import com.PowerMockitoProduction;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.when;


@RunWith(PowerMockRunner.class)
@PrepareForTest(PowerMockitoProduction.class)
public class PowerMockitoTest {

    @Test(expected = UnsupportedOperationException.class)
    public void test() throws Exception {

        HttpClient mock = PowerMockito.mock(HttpClient.class);
        when(mock.executeMethod(any(HttpMethod.class))).thenThrow(UnsupportedOperationException.class);

        new PowerMockitoProduction().createClient();

    }
}

这个测试失败了:

java.lang.Exception: Unexpected exception, expected<java.lang.UnsupportedOperationException> but was<java.lang.IllegalArgumentException>

这是我的生产代码:

package com;

import org.apache.commons.httpclient.HttpClient;

import java.io.IOException;


public class PowerMockitoProduction {

    public void createClient() throws IOException {
        HttpClient client = new HttpClient();
        client.executeMethod(null);
        System.out.println(client);
    }

}

使用我的调试器,我可以看到 client 不像我预期的那样是模拟的。

我也尝试过使用:

Object mock = PowerMockito.whenNew(HttpClient.class).withNoArguments().getMock();

但由于某种原因,这会返回一个未完全构造的模拟。我也试过这个:

HttpClient mock = PowerMockito.whenNew(HttpClient.class).withNoArguments().thenReturn(mock(HttpClient.class)).getMock();

但这给了我一个ClassCastException 在那条线上。那么,用 PowerMockito 完全模拟一个类的正确方法是什么?

与此示例所暗示的不同,我尝试模拟 HttpClient 的原因是我可以稍后调用 verify

【问题讨论】:

  • 完全模拟一个类是什么意思。抱歉,我多次回答您的问题,但无法理解那部分。
  • @RohitJain 我正在寻找 Mockito 的 mock 方法的 PowerMockito 版本。
  • 你不需要在thenReturn()之后调用getMock()方法。
  • @RohitJain 好的,thenReturn 不会返回 HttpClient。我想在模拟中verify

标签: java unit-testing mockito powermock powermockito


【解决方案1】:

您不需要调用getMock() 方法来取回模拟对象。基本上,模拟一个HttpClient 的实例,将其存储在局部变量中并使用它:

@Test(expected=UnsupportedOperationException.class)
public void test() {
    HttpClient httpClient = mock(HttpClient.class);
    PowerMockito.whenNew(HttpClient.class).withNoArguments().thenReturn(httpClient);
    when(httpClient.executeMethod(any(HttpMethod.class))).thenThrow(UnsupportedOperationException.class);

    new PowerMockitoProduction().createClient();
    verify(httpClient).executeMethod(null);
}

【讨论】:

  • 这是您应该的方式吗?看起来有点绕。
  • 是的。您必须将模拟实例存储为局部变量或实例变量,并使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多