久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 mockMvc 檢查響應(yīng)正文中的 JSON

How to check JSON in response body with mockMvc(如何使用 mockMvc 檢查響應(yīng)正文中的 JSON)
本文介紹了如何使用 mockMvc 檢查響應(yīng)正文中的 JSON的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

這是我在控制器中的方法,由 @Controller

This is my method inside my controller which is annotated by @Controller

@RequestMapping(value = "/getServerAlertFilters/{serverName}/", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JSONObject getServerAlertFilters(@PathVariable String serverName) {
        JSONObject json = new JSONObject();
        List<FilterVO> filteredAlerts = alertFilterService.getAlertFilters(serverName, "");
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(filteredAlerts);
        json.put(SelfServiceConstants.DATA, jsonArray);
        return json;
    }

我期待 {"data":[{"useRegEx":"false","hosts":"v2v2v2"}]} 作為我的 json.

I am expecting {"data":[{"useRegEx":"false","hosts":"v2v2v2"}]} as my json.

這是我的 JUnit 測(cè)試:

And this is my JUnit test:

@Test
    public final void testAlertFilterView() {       
        try {           
            MvcResult result = this.mockMvc.perform(get("/getServerAlertFilters/v2v2v2/").session(session)
                    .accept("application/json"))
                    .andDo(print()).andReturn();
            String content = result.getResponse().getContentAsString();
            LOG.info(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

這是控制臺(tái)輸出:

MockHttpServletResponse:
              Status = 406
       Error message = null
             Headers = {}
        Content type = null
                Body = 
       Forwarded URL = null
      Redirected URL = null
             Cookies = []

甚至 result.getResponse().getContentAsString() 也是一個(gè)空字符串.

Even result.getResponse().getContentAsString() is an empty string.

有人可以建議如何在我的 JUnit 測(cè)試方法中獲取我的 JSON,以便我可以完成我的測(cè)試用例.

Can someone please suggest how to get my JSON in my JUnit test method so that I can complete my test case.

推薦答案

我使用 TestNG 進(jìn)行單元測(cè)試.但是在 Spring Test Framework 中,它們看起來都很相似.所以我相信你的測(cè)試如下所示

I use TestNG for my unit testing. But in Spring Test Framework they both looks similar. So I believe your test be like below

@Test
public void testAlertFilterView() throws Exception {
    this.mockMvc.perform(get("/getServerAlertFilters/v2v2v2/").
            .andExpect(status().isOk())
            .andExpect(content().json("{'data':[{'useRegEx':'false','hosts':'v2v2v2'}]}"));
    }

如果你想檢查檢查 json 鍵和值,你可以使用 jsonpath.andExpect(jsonPath("$.yourKeyValue", is("WhatYouExpect")));

If you want check check json Key and value you can use jsonpath .andExpect(jsonPath("$.yourKeyValue", is("WhatYouExpect")));

您可能會(huì)發(fā)現(xiàn)content().json() 無法解決請(qǐng)?zhí)砑?em class="showen">

You might find thatcontent().json() are not solveble please add

導(dǎo)入靜態(tài) org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

這篇關(guān)于如何使用 mockMvc 檢查響應(yīng)正文中的 JSON的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對(duì)象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 日韩精品久久久久久 | 久热综合| 手机看片久久 | www.毛片 | 成人短视频在线观看 | 欧美日韩久久久 | 成人毛片网站 | 国产精品91在线 | 日本一区二区不卡视频 | 国产剧情在线 | 亚洲一区二区免费 | 一级黄片毛片 | 一区二区三区在线免费 | 午夜在线影院 | 成人不卡视频 | 免费视频久久久 | 久久久久久久成人 | 四虎影视大全 | 欧美性猛交xx | 国产福利视频 | 日韩午夜影院 | 日韩精品中文字幕在线观看 | 日韩欧美高清视频 | 免费av网站在线观看 | 欧美成人精品一区二区三区在线看 | 中文字幕系列 | 亚洲在线| a级片免费观看 | 欧美日韩亚洲一区二区 | 亚洲成人av在线播放 | 午夜国产在线观看 | 欧美一区二区精品 | 成人aaa| a级片免费在线观看 | 国产理论片| 国产精品2区 | 欧美一级欧美三级在线观看 | 极品尤物一区二区三区 | 欧美日韩视频在线 | 久久性色 | 亚洲123区 |