模拟打开假装反应身体

时间:2019-04-25 09:06:30

标签: java junit junit4 feign

我有一个显示的代码

Loaded custom extension 'BotFrameworkConfiguration'
Loaded custom extension 'SendGridConfiguration'
Loaded custom extension 'EventGridExtensionConfig'
A ScriptHost error has occurred
mscorlib: Could not load file or assembly 'file://\\Mac\Home\Documents\XXX\bin\Debug\net461\bin\YYY.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Stopping Host

现在要嘲笑我正在使用Mockito。我已经嘲笑了此响应的标头,但无法模仿正文。对如何实现感到困惑?尝试过但失败了。

MyDefinition databricksResponseBody = null; ResponseBody = gson.fromJson(Response.body().asReader(), MyDefinition.class);

2 个答案:

答案 0 :(得分:0)

一种方法是不仅模拟Response,而且模拟内部对象来“链接”模拟。

/* create the response mock */
Response response = mock(Response.class);

/* create the body mock */
Response.Body body = mock(Response.Body.class);

/* specify that the mocked body should be returned */
when(response.body()).thenReturn(body);

/* more mocking or do something with the response */
...

使用这种技术,您应该能够为任何用例操纵模拟的Body

答案 1 :(得分:0)

我们不能模拟 feign.Response,因为这是一个 Final 类!最好我们应该使用 powerMock else 给出自己的假实现。