有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是类组织。阿帕奇。http。impl。客户未为测试做好准备的HttpClient

组织。powermock。应用程序编程接口。莫基托。ClassNotPreparedException。班级组织。阿帕奇。http。impl。客户HttpClient未准备好进行测试

这是我的测试课

public class KMSHttpClientImplTest {


@InjectMocks
private KMSHttpClient kmsHttpClient;

@Mock
private HttpClient httpClient;

@Mock
private HttpGet httpGet;

/**
 * Initial SetUp Method
 */
@Before
public void setUp() throws KMSClientException, IOException {
    kmsHttpClient = new KMSHttpClientImpl();
    initMocks(this);
}

//@Test
public void testPostRequest () throws IOException {
    OrganizationRequest request = getOrganizationRequest();
    HttpResponse response = prepareResponse(200);
    //Mockito.when(mockedHttpClient.execute(Mockito.any())).thenReturn(response);
    HttpResponse httpResponse = kmsHttpClient.postRequest("https://www.google.co.in/" , new StringEntity(request.toString()));
    Assert.assertEquals( 200, httpResponse.getStatusLine().getStatusCode());
}

@Test
public void testGetRequest () throws IOException {
    HttpResponse response = prepareResponse(200);
       PowerMockito.mockStatic(HttpClients.class);
       when(HttpClients.custom()).thenReturn((HttpClientBuilder) httpClient);
    Mockito.when(httpClient.execute(httpGet)).thenReturn(response);
    HttpResponse httpResponse = kmsHttpClient.getRequest("https://www.test.com/");
    Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
}

public OrganizationRequest getOrganizationRequest () {
    return OrganizationRequest.builder().id("test").build();
}

private HttpResponse prepareResponse(int expectedResponseStatus) {
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(
            new ProtocolVersion("HTTP", 1, 1), expectedResponseStatus, ""));
    response.setStatusCode(expectedResponseStatus);
    return response;
}
}

这是我的java课程

 public class KMSHttpClientImpl implements KMSHttpClient
 {

/**
 * KMS ConnectionManager Instance.
 */
private final KMSHttpConnectionManager kmsHttpConnectionManager =
        new KMSHttpConnectionManager ();

/**
 * HttpClient object.
 */
private final HttpClient httpClient;

/**
 * KMSHttpClient constructor.
 */
public KMSHttpClientImpl ()
{
    // TODO PoolingHttpClientConnectionManager object should be closed after use.
    final PoolingHttpClientConnectionManager connectionManager =
            kmsHttpConnectionManager.getConnectionManager();
    httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setRetryHandler(kmsHttpConnectionManager.retryHandler(RETRY_COUNT)).build();
}

/**
 * Method to execute Post request.
 * @param url
 * @param stringEntity
 * @return HttpResponse
 * @throws IOException
 */
public HttpResponse postRequest (final String url,
                                 final StringEntity stringEntity) throws IOException
{
    final HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(stringEntity);
    return httpClient.execute(httpPost);
}

/**
 * Method to execute Get request.
 * @param url
 * @return HttpResponse
 * @throws IOException
 */
public HttpResponse getRequest (final String url) throws IOException
{
    final HttpGet httpGet = new HttpGet(url);
    return httpClient.execute(httpGet);
}
}

我在这条线路上出错了。mockStatic(HttpClients.class);说 组织。powermock。应用程序编程接口。莫基托。ClassNotPreparedException。班级组织。阿帕奇。http。impl。客户HttpClient未准备好进行测试


共 (0) 个答案