如何使用 mockito 和 junit 测试此功能?
How do I test this function using mockito and junit?
我是 mockito 和 junit5 的新手。我正在尝试测试以下功能:
public boolean checkFunction(String element) {
CloseableHttpClient client = HttpClients.createDefault();
String uri = "any url im hitting";
HttpPost httpPost = new HttpPost(uri);
String json = element;
StringEntity entity;
try {
entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Authorization", "any token");
CloseableHttpResponse response = client.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
client.close();
if (responseBody.contains("any string i wanna check"))
return true;
} catch (Exception e) {
return false;
}
return false;
}
我尝试了下面的代码,但我无法获得完整的代码覆盖率。我也不认为这是正确的方法。
@Test
public void testCheckFunction() throws Exception {
when(mockClass.checkFunction(Mockito.anyString())).thenReturn(false);
assertEquals(false, mockclass.checkFunction("dummy"));
}
谁能帮我解决这个问题?
谢谢!
首先,您必须重构代码以获得更好的可测试性:
public class Checker {
private final CloseableHttpClient client;
public Checker(CloseableHttpClient client) {
this.client = client;
}
public boolean checkFunction(String element) {
String uri = "http://example.com";
HttpPost httpPost = new HttpPost(uri);
String json = element;
StringEntity entity;
try {
entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Authorization", "any token");
CloseableHttpResponse response = client.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
client.close();
if (responseBody.contains("any string i wanna check"))
return true;
} catch (Exception e) {
return false;
}
return false;
}
}
请注意,依赖项(即必须在测试中模拟的东西)现在是通过构造函数注入的。这样,在单元测试 class:
时,它可以很容易地被模拟替换
class CheckerTest {
private final CloseableHttpClient clientMock = Mockito.mock(CloseableHttpClient.class);
private final Checker checker = new Checker(clientMock);
@Test
public void testCheckFunction() throws Exception {
when(clientMock.execute(any(HttpPost.class))).thenThrow(new RuntimeException("Oops!"));
assertFalse(checker.checkFunction("dummy"));
}
}
我是 mockito 和 junit5 的新手。我正在尝试测试以下功能:
public boolean checkFunction(String element) {
CloseableHttpClient client = HttpClients.createDefault();
String uri = "any url im hitting";
HttpPost httpPost = new HttpPost(uri);
String json = element;
StringEntity entity;
try {
entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Authorization", "any token");
CloseableHttpResponse response = client.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
client.close();
if (responseBody.contains("any string i wanna check"))
return true;
} catch (Exception e) {
return false;
}
return false;
}
我尝试了下面的代码,但我无法获得完整的代码覆盖率。我也不认为这是正确的方法。
@Test
public void testCheckFunction() throws Exception {
when(mockClass.checkFunction(Mockito.anyString())).thenReturn(false);
assertEquals(false, mockclass.checkFunction("dummy"));
}
谁能帮我解决这个问题? 谢谢!
首先,您必须重构代码以获得更好的可测试性:
public class Checker {
private final CloseableHttpClient client;
public Checker(CloseableHttpClient client) {
this.client = client;
}
public boolean checkFunction(String element) {
String uri = "http://example.com";
HttpPost httpPost = new HttpPost(uri);
String json = element;
StringEntity entity;
try {
entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Authorization", "any token");
CloseableHttpResponse response = client.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
client.close();
if (responseBody.contains("any string i wanna check"))
return true;
} catch (Exception e) {
return false;
}
return false;
}
}
请注意,依赖项(即必须在测试中模拟的东西)现在是通过构造函数注入的。这样,在单元测试 class:
时,它可以很容易地被模拟替换class CheckerTest {
private final CloseableHttpClient clientMock = Mockito.mock(CloseableHttpClient.class);
private final Checker checker = new Checker(clientMock);
@Test
public void testCheckFunction() throws Exception {
when(clientMock.execute(any(HttpPost.class))).thenThrow(new RuntimeException("Oops!"));
assertFalse(checker.checkFunction("dummy"));
}
}