【问题标题】:Mocking method with javax.ws.rs.core.Response and assertion使用 javax.ws.rs.core.Response 和断言的模拟方法
【发布时间】:2019-04-09 06:46:39
【问题描述】:

我有一个实现/调用另一个方法的主要方法。我正在为 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;
}

我正在尝试在这里编写一个测试用例,以便我可以模拟 getName 的响应以返回非 200 响应。

@Test
public void testGetAccount(){
when(getName).thenReturn(...);//How do I mock this?
String result = getAccount(anyString, anyString);
assertNotEqual(Https.Ok);
}

如何在此处模拟 getName 方法的响应?

【问题讨论】:

  • 您的当前形式的测试代码将无法编译,即使假设已填写空白...

标签: java unit-testing mocking response testcase


【解决方案1】:

试试:

 MyResponseObject myResponseObject= MyResponseObject(); 
    Response 
     response=Response.status(Response.Status.OK).entity(myResponseObject).build(); 
     when(getName(anyString, anyString)).thenReturn(response);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多