package com.springboot.demo.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**web
@Version 1.0br/>**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class LeaveControllerTest {
private MockMvc mockMvc;spring
@Before
public void setUp() throws Exception{
mockMvc = MockMvcBuilders.standaloneSetup(new LeaveController()).build();
}springboot
@Test
public void hello() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/level/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
說明:
new LeaveController()表示須要測試的控制器類
MockMvcRequestBuilders.get("/level/hello"):請求的方法ide