Skip to content

Commit 8ffd2d3

Browse files
authored
Merge pull request #24 from boylegu/issue/21/unit_test
close #21 init unit test in spring
2 parents 3d7fab0 + 1c8eef4 commit 8ffd2d3

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

src/test/java/com/boylegu/springboot_vue/SpringbootVueApplicationTests.java

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,73 @@
22

33
import org.junit.Test;
44
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;
76

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
1031
public class SpringbootVueApplicationTests {
1132

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", "test@qq.com");
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+
}
1573

1674
}

0 commit comments

Comments
 (0)