如何模拟Jsoup发布呼叫并以不断的响应回复

时间:2016-08-25 17:51:12

标签: java cucumber jsoup mockito powermock

我正在尝试单元测试[使用Cucumber]以下代码库

a

我尝试了以How to test method Jsoup.connect (static) using PowerMock?

为灵感的以下解决方案
final Connection connection = Jsoup.connect(PERFORMACE_TESTING_POST_URL);
  connection
      .followRedirects(false)
      .method(Method.POST);
  final org.jsoup.Connection.Response response = connection.execute();
  final String ptJobLocation = response.header(HTTP_HEADER_LOCATION);
  message = "Performance Testing Job Triggered. It is available at " + ptJobLocation;

但是我一直收到以下错误

//
// Mock the Jsoup mockConnection
//
final Connection mockConnection = Mockito.mock(Connection.class);
final Response mockResponse = Mockito.mock(Response.class);
PowerMockito.mockStatic(Jsoup.class);

//
// Mock jsoup connect
//
PowerMockito
    .when(Jsoup.connect(Constants.PERFORMACE_TESTING_POST_URL))
    .thenReturn(mockConnection);

//
// Mock connection.execute
//
Mockito
    .when(mockConnection.execute())
    .thenReturn(mockResponse);

//
// Mock header Retreival from Response
//
PowerMockito
    .when(mockResponse.header(anyString()))
    .thenReturn("Test Response");

对此有任何帮助将不胜感激。

使用的Libs如下

  1. Jsoup:1.9.2
  2. 黄瓜小黄瓜:1.2.4
  3. junit:4.12
  4. power-mock:1.6.4
  5. mockito:1.10.19

0 个答案:

没有答案