[英]Mocking method with javax.ws.rs.core.Response and assertion
I have a main method that implements/calls another method.我有一个实现/调用另一个方法的主要方法。 I'm writing a test case for the main method where I have to mock the response of the calling method.
我正在为 main 方法编写一个测试用例,我必须在其中模拟调用方法的响应。
public String getAccount(String add, String sub) {
...
Response r = getService(add, sub);
...
}
public Response getName(String add, String sub) {
...
Response r = WebTarget.path(pathString).queryParam("aaa", "xxxx").queryParam("byId", add)
.request().header("accept", "json")
.header("Authorization",token).get();
return r;
}
I'm trying to write a test case here so that I can mock the response of getName to return non-200 response.我试图在这里编写一个测试用例,以便我可以模拟 getName 的响应以返回非 200 响应。
@Test
public void testGetAccount(){
when(getName).thenReturn(...);//How do I mock this?
String result = getAccount(anyString, anyString);
assertNotEqual(Https.Ok);
}
How do I mock the response of getName method here?我如何在这里模拟 getName 方法的响应?
Try:尝试:
MyResponseObject myResponseObject= MyResponseObject();
Response
response=Response.status(Response.Status.OK).entity(myResponseObject).build();
when(getName(anyString, anyString)).thenReturn(response);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.