gpt4 book ai didi

java - Mockito ArgumentCaptor 在 verify() 上给出 NullpointerException

转载 作者:行者123 更新时间:2023-12-01 04:29:22 27 4
gpt4 key购买 nike

我目前正在尝试为我们的 Servlet 创建测试。我 mock 过 HttpServletRequest , HttpServletResponse和一个用作数据库处理程序的对象。在对 doPost 的测试中方法,我的愿望是比较来自 Json 对象的值和发送到数据库对象的 ArrayList 的值。

小服务程序:

public class WidgetStatusServlet extends HttpServlet {

private DBController db = new DBController();

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
JsonParser parser = new JsonParser();
JsonElement tradeElement = parser.parse(request.getParameter("widgetdata"));
JsonArray json = tradeElement.getAsJsonArray();
Gson gson = new Gson();
Type listType = new TypeToken<List<WidgetStatus>>() {
}.getType();
ArrayList<WidgetStatus> widgets = gson.fromJson(json, listType);

Iterator<WidgetStatus> iterator = widgets.iterator();
System.out.println("Widgets");
while (iterator.hasNext()) {
WidgetStatus node = (WidgetStatus) iterator.next();
System.out.println(node);
}

db.addWidgetStatus(widgets);

}

考试:
public class WidgetStatusServletTest {

@Captor
private ArgumentCaptor<ArrayList<WidgetStatus>> captor;

private DBController dbMock = mock(DBController.class);
private HttpServletRequest request = mock(HttpServletRequest.class);
private HttpServletResponse response = mock(HttpServletResponse.class);
Repository repository = mock(Repository.class);`


@Test
public void doPost_ShouldVerifyWidgetsIsProvided() throws Exception {
final WidgetStatus widget1 = new WidgetStatus("id1", "div_id1", "5", "5", "1", "2", false);
final WidgetStatus widget2 = new WidgetStatus("id2", "div_id2", "2", "1", "3", "1", true);

when(request.getParameter("widgetdata")).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
ArrayList<WidgetStatus> array = new ArrayList<WidgetStatus>();
array.add(widget1);
array.add(widget2);
String json = new Gson().toJson(array);
return json;
}
});
new WidgetStatusServlet().doPost(request, response);
verify(dbMock).addWidgetStatus(captor.capture());
assertNotNull(captor.getAllValues());


}

当我运行测试时,它给了我 NullpointerException在线
        verify(dbMock).addWidgetStatus(captor.capture());

我究竟做错了什么?我查看了几个与 ArgumentCaptor 用法相同的示例。 .也许这只是一个小错误?

最佳答案

如果您使用 MockitoJUnitRunner运行您的测试,然后是 ArgumentCaptor将为您创建,并且无需显式初始化它。如果你这样做,那么你也可以使用 @Mock注释来创建您的模拟对象,也就是说,您不需要 mock不再使用静态方法。您的测试类(class)将像这样开始。

@RunWith(MockitoJUnitRunner.class)
public class WidgetStatusServletTest {

@Captor private ArgumentCaptor<ArrayList<WidgetStatus>> captor;
@Mock private DBController mockController;
@Mock private HttpServletRequest mockRequest;
@Mock private HttpServletResponse mockResponse;
@Mock private Repository mockRepository;

This question解决是否使用 MockitoJUnitRunner的问题,或显式调用 MockitoAnnotations.initMocks .

只是一个旁注 - 如果您使用变量名称清楚地表明您的对象是模拟的,那么您的测试方法将更容易阅读,就像我在这里所做的那样。否则,在具有许多不同变量的长测试类中,很容易忘记哪些变量引用了模拟,哪些引用了“真实”对象。

关于java - Mockito ArgumentCaptor 在 verify() 上给出 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23310350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com