springboot测试

package club.iyousi.controller;

import javafx.application.Application;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void listUser() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/users"))
                .andExpect(MockMvcResultMatchers.status().isOk())//通过
                .andExpect(MockMvcResultMatchers.content().string("[]"));//期望结果是一个值
    }

    @Test
    public void post() throws Exception {
        mvc.perform(MockMvcRequestBuilders.post("/users").param("id","1").param("name","sfz").param("age","12"))
                .andExpect(MockMvcResultMatchers.status().isOk())//通过
                .andExpect(MockMvcResultMatchers.content().string("success"));//期望结果是一个值
    }
}

 

posted @ 2018-03-08 21:57  宋发准  阅读(146)  评论(0编辑  收藏  举报