如何模拟实现net.oauth.http.HttpClient的HttpClient4?

时间:2019-04-05 03:15:18

标签: java maven intellij-idea junit powermock

我有这两行代码。我正在尝试模拟这些对象和方法,但是intellij和powermock不喜欢它。

代码段:

final HttpClient httpClient = new HttpClient4();
OAuthClient oAuthClient = new OAuthClient(httpClient);

PowerMock Junit代码段:

class Test {
  @Mock
  private HttpClient httpClientMock;
  @Mock
  private OAuthClient oAuthClientMock;
  @Before
  public void setUp() throws Exception {
   whenNew(HttpClient4.class).withNoArguments().thenReturn((HttpClient4) httpClientMock);
 whenNew(OAuthClient.class).withArguments(httpClientMock).thenReturn(oAuthClientMock);
   }
}

错误:

java.lang.ClassCastException: 
net.oauth.http.HttpClient$$EnhancerByMockitoWithCGLIB$$63820671 cannot be cast to net.oauth.client.httpclient4.HttpClient4

如果我不将httpClientMock强制转换为HttpClient4,则出现此错误。 错误:

Cannot resolve method 'thenReturn(net.oauth.http.HttpClient)'

不确定我可以使用其他什么方法来模拟这个。

0 个答案:

没有答案