java SpringMVC MockMvc测试和JSON路径检查

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java SpringMVC MockMvc测试和JSON路径检查相关的知识,希望对你有一定的参考价值。

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = {"classpath:/spring/applicationContext-*.xml"})  
class JUnitServiceBase extends AbstractTransactionalJUnit4SpringContextTests {  
  
    /** 
     * <b>Summary: </b> 复写方法 setDataSource 
     *  
     * @param dataSource 
     * @see org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests#setDataSource(javax.sql.DataSource) 
     */  
    @Override  
    @Resource(name = "dataSource")  
    public void setDataSource(DataSource dataSource) {  
        super.setDataSource(dataSource);  
    }  
} 



/**
 * Created by Administrator on 2016/12/14.
 */
@WebAppConfiguration
public class MemberControllerTest extends JUnitServiceBase{

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() throws Exception {
        this.mockMvc = webAppContextSetup(this.wac).build();
    }


    @Test
    public void getHomePage() throws Exception {


        getHomePageTemplate("","0","").andExpect(status().isOk())
                .andDo(print())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                //check memberlist is null empty
                .andExpect(MockMvcResultMatchers.jsonPath("$.memberList").isArray())
                .andExpect(MockMvcResultMatchers.jsonPath("$.canTalklist").isArray())
                .andExpect(MockMvcResultMatchers.jsonPath("$.cityRadioList").isArray())
                .andReturn();
    }

    @Test
    public void getHomePageWhenMemberListIsEmpty() throws Exception {
        getHomePageTemplate("58","20","373,137,191,130,297,138,124,293,348,49,317,118,344,318,58,341").andExpect(status().isOk())
                .andDo(print())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(MockMvcResultMatchers.jsonPath("$.memberList",is(empty())))
                .andExpect(MockMvcResultMatchers.jsonPath("$.canTalklist",is(empty())))
                .andExpect(MockMvcResultMatchers.jsonPath("$.cityRadioList",is(empty())))
                .andReturn();
    }



    @Test
    public void getHomePageAfterLogin() throws Exception {
        getHomePageTemplate("58","0","373,137,191,130,297,138,124,293,348,49,317,118,344,318,58,341").andExpect(status().isOk())
                .andDo(print())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                //check memberlist is null empty
                .andExpect(MockMvcResultMatchers.jsonPath("$.memberList",is(not(empty()))))
                .andExpect(MockMvcResultMatchers.jsonPath("$.canTalklist",is(not(empty()))))
                .andExpect(MockMvcResultMatchers.jsonPath("$.cityRadioList").isArray())
                .andReturn();
//                .andExpect(content().json("{\"memberList\":\"\",\"canTalklist\":\"\",\"cityRadioList\":\"\"}"))
//        String content = result.getResponse().getContentAsString();
    }


    private ResultActions getHomePageTemplate(String memeberId,String page,String exludesIDs) throws Exception {
        return this.mockMvc.perform(
                get("/web/member/getHomePage")
                        .param("memberId",memeberId)
                        .param("params","{sort:'',age:'18,60',isHasPhoto:'',isHasRadio:'',city:'青岛市',vip:'',high:'150,200',weight:'30,120',label:''}")
                        .param("type",page)
                        .param("memberIdS",exludesIDs));
    }



}

以上是关于java SpringMVC MockMvc测试和JSON路径检查的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC 测试,MockMVC:方便地将对象与 JSON 进行转换

Spring Boot MVC 测试 - MockMvc 始终为空

org.springframework.web.servlet.DispatcherServlet noHandlerFound 通过 MockMvc 测试时

使用 Spring mockMvc 测试可选路径变量

Java MockMvc 测试和匹配 Json 字符串

java Spring Test MockMvc测试实例