tianyunperfect пре 5 година
родитељ
комит
0708597d3f

+ 4 - 0
app/src/main/java/com/alvin/controller/AppController.java

@@ -33,4 +33,8 @@ public class AppController {
         PageResult<User> pageResult = new PageResult<>(1, 10, 100L, Collections.singletonList(user));
         return Result.success(pageResult);
     }
+
+    public String test() {
+        return "123";
+    }
 }

+ 20 - 0
app/src/test/java/com/alvin/controller/AppControllerTest.java

@@ -0,0 +1,20 @@
+package com.alvin.controller;
+
+import org.junit.Test;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+
+public class AppControllerTest extends TestBase {
+
+    @Test
+    public void testFindOne() throws Exception {
+        mockMvc.perform(
+                MockMvcRequestBuilders.get("/user/findOne")
+                .contentType(MediaType.APPLICATION_JSON_UTF8)
+        )
+                .andExpect(MockMvcResultMatchers.status().isOk())
+                .andReturn();
+        //System.out.println(mvcResult.getResponse().getContentAsString());
+    }
+}

+ 37 - 0
app/src/test/java/com/alvin/controller/TestBase.java

@@ -0,0 +1,37 @@
+package com.alvin.controller;
+
+import org.junit.After;
+import org.junit.Before;
+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.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+
+
+/**
+ * 测试基础类,建议测试service层
+ *
+ * @author tianyunperfect
+ * @date 2020/05/20
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest
+@WebAppConfiguration
+@AutoConfigureMockMvc
+public class TestBase {
+    @Autowired
+    protected MockMvc mockMvc;
+
+    @Before
+    public void init() {
+        System.out.println("开始测试-----------------");
+    }
+
+    @After
+    public void after() {
+        System.out.println("测试结束-----------------");
+    }
+}