如何为我的模拟实例化一个 unirest HttpResponse<JsonNode>?
How do I instantiate an unirest HttpResponse<JsonNode> for my mock?
假设我有一个名为 Api 的 class,它有一个方法:
public class Api{
public HttpResponse<JsonNode> request() {
try {
return Unirest.get("http://localhost:8080").header("accept", "application/json").asJson();
} catch (UnirestException e) {
throw new RuntimeException(e);
}
}
}
我有一个 class:
public class Dao(){
private Api api;
public Dao(Api api){
this.api = api;
}
public Integer test(){
Integer result = api.request().getInteger("result");
return result + 100;
}
}
在我的测试中,我想根据 API.request 方法 returns 的响应来测试我的业务逻辑。
类似于:
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.stub;
import org.json.JSONObject;
import com.mashape.unirest.http.HttpResponse;
public class ApiTest {
private API api = mock(API.class);
public void test() {
HttpResponse<JsonNode> response = null;
JSONObject result = new JSONObject();
response.getBody().getObject();
stub(api.request("")).toReturn(response);
Dao dao = new Dao(api);
assertTrue(dao.test() > 100);
}
}
如何使用主体“{ number: 10 }”实例化 HttpResponse 以便能够 return 它与模拟?
适用于:
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.json.JSONObject;
HttpResponseFactory factory = new DefaultHttpResponseFactory();
org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);
JSONObject json = new JSONObject();
json.put("result", "10");
response.setEntity(new StringEntity(json.toString()));
com.mashape.unirest.http.HttpResponse<JsonNode> httpResponse = new HttpResponse<JsonNode>(response, JsonNode.class);
我是这样做的:
public class Dao() {
private Api api;
public Dao(Api api){
this.api = api;
}
public Integer test(){
// this is probably not good style
// should iterate through pulling out each piece
Integer result = api.request().getBody().getObject().getInteger("result");
return result + 100;
}
}
public class ApiTest {
private API api = mock(API.class);
public void test() {
JsonNode json = new JsonNode("{\"result\":10}");
HttpResponse<JsonNode> mockResponse = mock(HttpResponse.class);
when(mockResponse.getCode()).thenReturn(200);
when(mockResponse.getBody()).thenReturn(json);
when(api.request(anyString())).thenReturn(mockResponse);
Dao dao = new Dao(api);
// this should be done more carefully as well, check code/body/etc..
assertTrue(dao.test() > 100);
}
}
import org.mockito.Mock; // used for mock
//On class
@RunWith(PowerMockRunner.class)
@PrepareForTest(Unirest.class)
// Mock the variable using mockito
@Mock
private HttpResponse<String> getPost;
@Mock
private HttpResponse<String> httpResponse;
@Mock
private HttpRequestWithBody httpRequestWithBody;
@Mock
private RequestBodyEntity requestBodyEntity;
//In before setup function
PowerMockito.mockStatic(Unirest.class);
// In test function - used mock variables in thenReturn and got the required mocked outcome
when(Unirest.post(wdUrl + "/")).thenReturn(httpRequestWithBody);
when(httpRequestWithBody.header("Content-Type", "application/json")).thenReturn(httpRequestWithBody);
when(httpRequestWithBody.body(
"{\"name\":\"TESTER NAME\",\"email\":\"testuser@yopmail.com\",\"username\":\"testinguser\"}"))
.thenReturn(requestBodyEntity);
when(requestBodyEntity.asString()).thenReturn(httpResponse);
when(httpResponse.getStatus()).thenReturn(Integer.valueOf(200));
引用此 github 资源 - https://github.com/thejamesthomas/javabank/blob/master/javabank-client/src/test/java/org/mbtest/javabank/ClientTest.java
假设我有一个名为 Api 的 class,它有一个方法:
public class Api{
public HttpResponse<JsonNode> request() {
try {
return Unirest.get("http://localhost:8080").header("accept", "application/json").asJson();
} catch (UnirestException e) {
throw new RuntimeException(e);
}
}
}
我有一个 class:
public class Dao(){
private Api api;
public Dao(Api api){
this.api = api;
}
public Integer test(){
Integer result = api.request().getInteger("result");
return result + 100;
}
}
在我的测试中,我想根据 API.request 方法 returns 的响应来测试我的业务逻辑。
类似于:
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.stub;
import org.json.JSONObject;
import com.mashape.unirest.http.HttpResponse;
public class ApiTest {
private API api = mock(API.class);
public void test() {
HttpResponse<JsonNode> response = null;
JSONObject result = new JSONObject();
response.getBody().getObject();
stub(api.request("")).toReturn(response);
Dao dao = new Dao(api);
assertTrue(dao.test() > 100);
}
}
如何使用主体“{ number: 10 }”实例化 HttpResponse 以便能够 return 它与模拟?
适用于:
import org.apache.http.impl.DefaultHttpResponseFactory;
import org.json.JSONObject;
HttpResponseFactory factory = new DefaultHttpResponseFactory();
org.apache.http.HttpResponse response = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null);
JSONObject json = new JSONObject();
json.put("result", "10");
response.setEntity(new StringEntity(json.toString()));
com.mashape.unirest.http.HttpResponse<JsonNode> httpResponse = new HttpResponse<JsonNode>(response, JsonNode.class);
我是这样做的:
public class Dao() {
private Api api;
public Dao(Api api){
this.api = api;
}
public Integer test(){
// this is probably not good style
// should iterate through pulling out each piece
Integer result = api.request().getBody().getObject().getInteger("result");
return result + 100;
}
}
public class ApiTest {
private API api = mock(API.class);
public void test() {
JsonNode json = new JsonNode("{\"result\":10}");
HttpResponse<JsonNode> mockResponse = mock(HttpResponse.class);
when(mockResponse.getCode()).thenReturn(200);
when(mockResponse.getBody()).thenReturn(json);
when(api.request(anyString())).thenReturn(mockResponse);
Dao dao = new Dao(api);
// this should be done more carefully as well, check code/body/etc..
assertTrue(dao.test() > 100);
}
}
import org.mockito.Mock; // used for mock
//On class
@RunWith(PowerMockRunner.class)
@PrepareForTest(Unirest.class)
// Mock the variable using mockito
@Mock
private HttpResponse<String> getPost;
@Mock
private HttpResponse<String> httpResponse;
@Mock
private HttpRequestWithBody httpRequestWithBody;
@Mock
private RequestBodyEntity requestBodyEntity;
//In before setup function
PowerMockito.mockStatic(Unirest.class);
// In test function - used mock variables in thenReturn and got the required mocked outcome
when(Unirest.post(wdUrl + "/")).thenReturn(httpRequestWithBody);
when(httpRequestWithBody.header("Content-Type", "application/json")).thenReturn(httpRequestWithBody);
when(httpRequestWithBody.body(
"{\"name\":\"TESTER NAME\",\"email\":\"testuser@yopmail.com\",\"username\":\"testinguser\"}"))
.thenReturn(requestBodyEntity);
when(requestBodyEntity.asString()).thenReturn(httpResponse);
when(httpResponse.getStatus()).thenReturn(Integer.valueOf(200));
引用此 github 资源 - https://github.com/thejamesthomas/javabank/blob/master/javabank-client/src/test/java/org/mbtest/javabank/ClientTest.java