我如何测试resp.getOutputStream()

时间:2019-08-30 20:01:00

标签: java servlets mockito

我有要测试的servlet。我想测试Mockito的doGet。 在srvlet的主要代码信息中,转到PrintWriter的/ json页面。 在我的测试中,我想将要保存测试信息的文件保存在其中。我无法测试PrintWriter(我不知道如何)

我尝试使用

代替了测试中的PrintWriter
StringWriter sw = new StringWriter();
PrintWriter pw  =new PrintWriter(sw);
when(new PrintWriter(resp.getOutputStream())).thenReturn(pw);

这是我的Servlet操作

 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json");
        PrintWriter writer = new PrintWriter(resp.getOutputStream());
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(list);
        writer.append(jsonString);
        writer.flush();
    }

这是我无法通过的测试

@Test
    public void whenShouldCheckJsonSrevlet() throws ServletException, IOException {
        String fileWay = "D:\\CODE_WORK\\JAVA\\java-a-to-z\\lesson8Aplication\\src\\main\\java\\persistent\\test.txt";
        final JSONControllers servlet = new JSONControllers();
        HttpServletRequest req = mock(HttpServletRequest.class);
        HttpServletResponse resp = mock(HttpServletResponse.class);
        PrintWriter writer = new PrintWriter(new FileWriter(fileWay));
        BufferedReader reader = new BufferedReader(new FileReader(fileWay));
        when(new PrintWriter(resp.getOutputStream())).thenReturn(writer);
        when(req.getReader()).thenReturn(reader);
        servlet.doGet(req,resp);
        servlet.doPost(req,resp);
    }

java.lang.NullPointerException
    at java.io.Writer.<init>(Writer.java:88)
    at java.io.OutputStreamWriter.<init>(OutputStreamWriter.java:109)
    at java.io.PrintWriter.<init>(PrintWriter.java:148)
    at java.io.PrintWriter.<init>(PrintWriter.java:131)
    at servlets.JSONControllersTest.whenShouldCheckJsonSrevlet(JSONControllersTest.java:62)

3 个答案:

答案 0 :(得分:0)

您要传递的是映射器的writeValueAsString对象是什么“列表”? JSONControllersTest.java的哪一行是62?

答案 1 :(得分:0)

 private ConcurrentHashMap<UUID, User> list = new ConcurrentHashMap<UUID, User>();
    public JSONControllers() {
        DbLocationStore db = DbLocationStore.getInstance();
        for (int i = 0; i < db.size(); i++) {
            list.put(generateID(),db.findById(String.valueOf(i)));
        }
    }

工作原理。我有数据库。并根据数据形成列表并将其转换为JSON 今天,我这样解决我的问题:

之前

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json");
        PrintWriter writer = new PrintWriter(resp.getOutputStream());
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(list);
        writer.append(jsonString);
        writer.flush();
    }

之后

 @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json");
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(list);
        resp.getWriter().write(jsonString);
        resp.getWriter().flush();
    }

但我仍然很有趣,如何对此进行正交测试 PrintWriter writer =新的PrintWriter(resp.getOutputStream());

答案 2 :(得分:0)

目前,我的测试看起来像这样:

@Test

public void whenShouldCheckJsonSrevlet() throws ServletException, IOException {
    String fileWay = "D:\\CODE_WORK\\JAVA\\java-a-to-z\\lesson8Aplication\\src\\main\\java\\persistent\\test.txt";
    final JSONControllers servlet = new JSONControllers();
    HttpServletRequest req = mock(HttpServletRequest.class);
    HttpServletResponse resp = mock(HttpServletResponse.class);
    PrintWriter writer = new PrintWriter(new FileWriter(fileWay));
    BufferedReader reader = new BufferedReader(new FileReader(fileWay));
    when(resp.getWriter()).thenReturn(writer);
    when(req.getReader()).thenReturn(reader);
    servlet.doGet(req,resp);
    servlet.doPost(req,resp);
}