| 
2 | 2 | 
 
  | 
3 | 3 | import org.junit.Test;  | 
4 | 4 | import org.junit.runner.RunWith;  | 
5 |  | -import org.springframework.boot.test.context.SpringBootTest;  | 
6 |  | -import org.springframework.test.context.junit4.SpringRunner;  | 
 | 5 | +import org.junit.Before;  | 
7 | 6 | 
 
  | 
8 |  | -@RunWith(SpringRunner.class)  | 
9 |  | -@SpringBootTest  | 
 | 7 | +import org.springframework.test.context.ContextConfiguration;  | 
 | 8 | +import org.springframework.mock.web.MockServletContext;  | 
 | 9 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  | 
 | 10 | +import org.springframework.test.context.web.WebAppConfiguration;  | 
 | 11 | +import org.springframework.test.web.servlet.MockMvc;  | 
 | 12 | +import org.springframework.test.web.servlet.RequestBuilder;  | 
 | 13 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders;  | 
 | 14 | + | 
 | 15 | +import static org.hamcrest.Matchers.equalTo;  | 
 | 16 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;  | 
 | 17 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;  | 
 | 18 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;  | 
 | 19 | + | 
 | 20 | +import com.boylegu.springboot_vue.controller.MainController;  | 
 | 21 | + | 
 | 22 | +/**  | 
 | 23 | + * @author Boyle Gu  | 
 | 24 | + * @version 0.0.1  | 
 | 25 | + * @GitHub https://github.com/boylegu  | 
 | 26 | + */  | 
 | 27 | + | 
 | 28 | +@RunWith(SpringJUnit4ClassRunner.class)  | 
 | 29 | +@ContextConfiguration(classes = MockServletContext.class)  | 
 | 30 | +@WebAppConfiguration  | 
10 | 31 | public class SpringbootVueApplicationTests {  | 
11 | 32 | 
 
  | 
12 |  | -	@Test  | 
13 |  | -	public void contextLoads() {  | 
14 |  | -	}  | 
 | 33 | +    private MockMvc mvc;  | 
 | 34 | + | 
 | 35 | +    @Before  | 
 | 36 | +    public void setUp() throws Exception {  | 
 | 37 | +        mvc = MockMvcBuilders.standaloneSetup(  | 
 | 38 | +                new MainController()).build();  | 
 | 39 | +    }  | 
 | 40 | + | 
 | 41 | +    @Test  | 
 | 42 | +    public void testUserController() throws Exception {  | 
 | 43 | +//  	Test MainController  | 
 | 44 | +        RequestBuilder request = null;  | 
 | 45 | + | 
 | 46 | +        request = get("/api/persons/sex");  | 
 | 47 | +        mvc.perform(request)  | 
 | 48 | +                .andExpect(status().isOk())  | 
 | 49 | +                .andExpect(content().string(equalTo("[]")));  | 
 | 50 | + | 
 | 51 | +        request = get("/api/persons/");  | 
 | 52 | +        mvc.perform(request)  | 
 | 53 | +                .andExpect(status().isOk())  | 
 | 54 | +                .andExpect(content().string(equalTo("[]")));  | 
 | 55 | + | 
 | 56 | +        request = get("/api/persons/detail/1");  | 
 | 57 | +        mvc.perform(request)  | 
 | 58 | +                .andExpect(status().isOk())  | 
 | 59 | +                .andExpect(content().string(equalTo("[{\"id\":1,\"username\":\"test\",\"zone\":20}]")));  | 
 | 60 | + | 
 | 61 | +        request = put("/api/persons/detail/1")  | 
 | 62 | +                .param("phone", "111111")  | 
 | 63 | +                . param( "email",  "[email protected]");  | 
 | 64 | +        mvc.perform(request)  | 
 | 65 | +                .andExpect(content().string(equalTo("success")));  | 
 | 66 | + | 
 | 67 | +        request = get("/api/persons");  | 
 | 68 | +        mvc.perform(request)  | 
 | 69 | +                .andExpect(status().isOk())  | 
 | 70 | +                .andExpect(content().string(equalTo("[]")));  | 
 | 71 | + | 
 | 72 | +    }  | 
15 | 73 | 
 
  | 
16 | 74 | }  | 
0 commit comments