|
|
@@ -0,0 +1,124 @@
|
|
|
+package cn.seecoder.courselearning.controller;
|
|
|
+
|
|
|
+import cn.seecoder.courselearning.mapperservice.UserMapper;
|
|
|
+import cn.seecoder.courselearning.vo.RechargeOrderVO;
|
|
|
+import cn.seecoder.courselearning.vo.UserFormVO;
|
|
|
+import cn.seecoder.courselearning.vo.UserVO;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.ObjectWriter;
|
|
|
+import org.junit.FixMethodOrder;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.jupiter.api.AfterAll;
|
|
|
+import org.junit.jupiter.api.TestInstance;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.junit.runners.MethodSorters;
|
|
|
+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.http.MediaType;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+import org.springframework.test.context.web.WebAppConfiguration;
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
+import org.springframework.web.context.WebApplicationContext;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
|
+
|
|
|
+import static org.hamcrest.core.Is.is;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
+
|
|
|
+
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest
|
|
|
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
|
|
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|
|
+@WebAppConfiguration
|
|
|
+public class UserControllerTest {
|
|
|
+ UserVO userVO = new UserVO();
|
|
|
+ private MockMvc mockMvc;
|
|
|
+ @Autowired
|
|
|
+ private WebApplicationContext wac;
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test_0_login() throws Exception {
|
|
|
+ mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //构建MockMVC
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
|
|
|
+ UserFormVO userFormVO = new UserFormVO();
|
|
|
+ String phone = "10212345678";
|
|
|
+ String password = "123456";
|
|
|
+ userFormVO.setPhone(phone);
|
|
|
+ userFormVO.setPassword(password);
|
|
|
+ String requestJson = ow.writeValueAsString(userFormVO);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/user/login")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON).content(requestJson))
|
|
|
+ .andExpect(MockMvcResultMatchers.status().isOk())
|
|
|
+ .andExpect(MockMvcResultMatchers.content()
|
|
|
+ .json("{\n" +
|
|
|
+ " \"code\": 1,\n" +
|
|
|
+ " \"msg\": \"账号登陆成功!\",\n" +
|
|
|
+ " \"data\": {\n" +
|
|
|
+ " \"id\": 2,\n" +
|
|
|
+ " \"uname\": \"小明\",\n" +
|
|
|
+ " \"phone\": \"10212345678\",\n" +
|
|
|
+ " \"password\": null,\n" +
|
|
|
+ " \"picture\": null,\n" +
|
|
|
+ " \"balance\": 0,\n" +
|
|
|
+ " \"userRole\": \"STUDENT\",\n" +
|
|
|
+ " \"createTime\": \"2020-12-18T02:00:00.000+00:00\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test_1_getUser() throws Exception {
|
|
|
+ mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //构建MockMVC
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
|
|
|
+
|
|
|
+ mockMvc.perform(get("/user/2")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON))
|
|
|
+ .andExpect(MockMvcResultMatchers.status().isOk())
|
|
|
+ .andExpect(MockMvcResultMatchers.content()
|
|
|
+ .json("{\n" +
|
|
|
+ " \"id\": 2,\n" +
|
|
|
+ " \"uname\": \"小明\",\n" +
|
|
|
+ " \"phone\": \"10212345678\",\n" +
|
|
|
+ " \"password\": null,\n" +
|
|
|
+ " \"picture\": null,\n" +
|
|
|
+ " \"balance\": 0,\n" +
|
|
|
+ " \"userRole\": \"STUDENT\",\n" +
|
|
|
+ " \"createTime\": \"2020-12-18T02:00:00.000+00:00\"\n" +
|
|
|
+ "}"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test_2_rechargeAccount() throws Exception {
|
|
|
+ mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //构建MockMVC
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
|
|
|
+ RechargeOrderVO rechargeOrderVO = new RechargeOrderVO();
|
|
|
+ rechargeOrderVO.setUserId(2);
|
|
|
+ rechargeOrderVO.setValue(100);
|
|
|
+ rechargeOrderVO.setCreateTime(new Date());
|
|
|
+ String requestJson = ow.writeValueAsString(rechargeOrderVO);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/user/recharge")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON).content(requestJson))
|
|
|
+ .andExpect(MockMvcResultMatchers.status().isOk())
|
|
|
+ .andExpect(jsonPath("$.msg", is("充值成功")));
|
|
|
+
|
|
|
+ userMapper.decreaseBalance(2, 100);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|