无法使用Mockito模拟对象



我在KMSHttpClientImplTest类中模拟Httpclient。但它并没有受到嘲笑。我得到实际的Httpclient实例。我做错了什么吗?

这是我的testClass

public class KMSHttpClientImplTest {

@InjectMocks
private KMSHttpClient kmsHttpClient;
HttpClient httpClient;
HttpGet httpGet;
/**
* Initial SetUp Method
*/
@Before
public void setUp() throws KMSClientException {
kmsHttpClient = new KMSHttpClientImpl();
httpClient = Mockito.mock(HttpClient.class);
httpGet = Mockito.mock(HttpGet.class);
}
@Test
public void testPostRequest () throws IOException {
OrganizationRequest request = getOrganizationRequest();
HttpResponse response = prepareResponse(200);
Mockito.when(httpClient.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);
Mockito.when(httpClient.execute(httpGet)).thenReturn(response);
HttpResponse httpResponse = kmsHttpClient.getRequest("https://www.test.co.in/");
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;
}

}

这是我的实际实现类
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.
// TODO This needs to be either singleton or should be kept in static block
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);
}

}

我在KMSHttpClientImplTest类中嘲笑Httpclient。但它并没有受到嘲笑。我得到实际的Httpclient实例。我做错了什么吗?

在您的示例中,您声明kmsHttpClient = new KMSHttpClientImpl();的新实例,因此您永远不会获得kmsHttpClient的mock。试着去做:

@InjectMocks
private KMSHttpClient kmsHttpClient;
@Mock
private HttpClient httpClient;
@Mock
private HttpGet httpGet;
/**
* Use @Before for junit4 or @BeforeEach for junit5
*/
@Before
public void setUp() throws KMSClientException {
initMocks(this);
}

最新更新