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

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

How to check JSON in response body with mockMvc(如何使用 mockMvc 檢查響應(yīng)正文中的 JSON)
本文介紹了如何使用 mockMvc 檢查響應(yīng)正文中的 JSON的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(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 測試:

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();
        }
    }

這是控制臺輸出:

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

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

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

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

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

推薦答案

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

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")));

您可能會發(fā)現(xiàn)content().json() 無法解決請?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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
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è)置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 日韩高清国产一区在线 | 久草中文在线 | 欧美黑人一区 | 国产精品欧美一区二区三区 | 欧美高清成人 | 精品二区视频 | 91视频在线 | 日韩高清一区二区 | 天天拍天天操 | 中文字幕亚洲视频 | 91 在线 | 欧美涩涩网 | 一级黄色片在线看 | 成人三级网址 | 国产黄色网址在线观看 | 亚洲一区二区在线 | 国产精品视频一二三区 | 青娱乐一区二区 | 一区二区av | 综合一区二区三区 | 中文字幕视频在线免费 | www.yw193.com| 99精品在线观看 | 久久精品一区二区 | 香蕉久久a毛片 | www.av7788.com| 91资源在线 | 天天射天天操天天干 | 99免费在线观看 | 超碰97人人人人人蜜桃 | 欧美另类视频在线 | 免费成人高清在线视频 | 久久亚洲经典 | 精品视频在线免费观看 | 天天操综合网 | 日韩精品亚洲专区在线观看 | 色视频www在线播放国产人成 | 精品蜜桃一区二区三区 | 欧美在线观看一区 | 亚洲综合色丁香婷婷六月图片 | 久久免费高清视频 |