我目前正在尝试为我们的Servlet创建测试。我嘲笑了HttpServletRequestHttpServletResponse和一个用作数据库处理程序的对象。在doPost方法的测试中,我希望比较Json对象的值和发送到数据库对象的ArrayList的值。

Servlet:

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静态方法。然后,您的测试课程将这样开始。

@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的问题。

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

07-25 20:15