spring boot 整合測試

愛吃大西瓜發表於2017-08-22
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
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.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

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

    /* 不帶引數  */
    @Test
    public void findGirls() throws Exception{
        mvc.perform(MockMvcRequestBuilders.get("/girl/girls"))  //get方法訪問url
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andDo(MockMvcResultHandlers.print());
    }

    /* 帶引數  */
    @Test
    public void addGirl() throws Exception{
        mvc.perform(MockMvcRequestBuilders.post("/girl/girls") //post方法訪問url
        .param("name", "瀟瀟").param("age", "17"))  //引數
        .andExpect(MockMvcResultMatchers.status().isOk())  //校驗訪問是否成功
        .andDo(MockMvcResultHandlers.print());  //列印返回引數
    }

}

相關文章