UserControllerTest.java 3.21 KB
package com.dituhui.pea.user;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

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



    @Test
    public void whenQueryUserSuccess() throws Exception {
        String result = mockMvc.perform(
                get("/v1/user/get").param("id", "40288ac97673e23f017673e26e3c0000")
                        .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenAddUserSuccess() throws Exception {
        String content = "{\"password\":\"123456789\",\"phone\":\"18600707186\",\"nickname\":\"地图旅行者\",\"account\":\"18600707186\",\"sex\":1,\"teamId\":\"40288ac97673e23f017673e26ed90001\"}";
        String result = mockMvc.perform(post("/v1/user/add").contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(content))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    @Test
    public void whenUpdateUserSuccess() throws Exception {
        String content = "{\"password\":\"123456789\",\"phone\":\"18600707187\",\"nickname\":\"地图王者\",\"account\":\"18600707187\",\"sex\":1,\"id\":\"40288ac97673e23f017673e26e3c0000\"}";
        String result = mockMvc.perform(post("/v1/user/update").contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(content))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


    public void whenDeleteUserSuccess() throws Exception {
        String result = mockMvc.perform(post("/v1/user/delete").param("id", "40288ac97673e23f017673e26e3c0000")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andExpect(jsonPath("$.success").value(true))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }


}