wuheng 2 years ago
parent
commit
207a23b128

+ 4 - 0
demo/src/main/java/com/lovecoding/springboot/demo/demo/DemoApplication.java

@@ -3,6 +3,10 @@ package com.lovecoding.springboot.demo.demo;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
+/**
+ * SpringBootApplication 这个注解是一个组合注解
+ * 主要标注 Springboot 启动类
+ */
 @SpringBootApplication
 public class DemoApplication {
 

+ 5 - 0
demo/src/main/java/com/lovecoding/springboot/demo/demo/DemoController.java

@@ -22,6 +22,11 @@ public class DemoController {
     @RequestMapping("/")
     public Long t(){
 
+        /**
+         * 这个服务是 我们自定义的  stater 对象
+         * 他会在 框架启动的时候就 初始化, 并读取配置档
+         * 自动装配依赖
+         */
         helloWorldService.print();
 
         return testMapper.counter();

+ 33 - 1
demo/src/test/java/com/lovecoding/springboot/demo/demo/DemoControllerTest.java

@@ -31,18 +31,39 @@ import static org.mockito.Mockito.when;
  * 集成测试 指的的 把相关功能点串联 测试
  */
 
+
+/**
+ * 测试分为两类
+ * 第一类是 框架全量加载 速度较慢, 但是走的是全流程
+ * @SpringBootTest
+ * @AutoConfigureMockMvc
+ * 第二类是 测试指定类
+ * @WebMvcTest(DemoController.class)
+ */
+
+
 //@SpringBootTest
 //@AutoConfigureMockMvc
 //@WebMvcTest(DemoController.class)
 @WebFluxTest(DemoController.class)
 class DemoControllerTest {
 
+    /**
+     * 自动注入 MockMvc 对象。需要配合 @AutoConfigureMockMvc 注解一起使用
+     */
     //@Resource
     //MockMvc mockMvc;
 
+    /**
+     * WebTestClient 测试工具 和 Mock 测试功能相近
+     * 属于异步调用
+     */
     @Resource
     WebTestClient webClient;
 
+    /**
+     * 创建MockBean 用于模拟 testMapper 返回值
+     */
     @MockBean
     TestMapper testMapper;
 
@@ -50,7 +71,7 @@ class DemoControllerTest {
 
     @Test
     public void webTest(){
-
+        // WebTestClient 测试用例 的 请求构造
         webClient.get().uri("/")
                 .accept(MediaType.APPLICATION_JSON_UTF8)
                 .exchange().expectStatus().isOk();
@@ -60,15 +81,26 @@ class DemoControllerTest {
     @Test
     public void t(  @Autowired MockMvc mockMvc  ) throws Exception {
 
+        /**
+         * 创建虚拟返回值
+         * 主要用于  还没有开发完成的工具类
+         * 可以先定义返回值
+         */
         when( testMapper.counter() ).thenReturn( 99999L );
 
+        //发起一个请求  参数是  请求方法和 请求路径
         MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.request(
                                 HttpMethod.GET, "/"
+                        //设置请求 类型
                         ).contentType(MediaType.APPLICATION_JSON_VALUE)
                 ).andDo(MockMvcResultHandlers.print())
+                //创建 断言 判断请求返回值 是否是 200  如果不通过则 测试失败
                 .andExpect(MockMvcResultMatchers.status().is(200))
+                //创建 断言  判断页面返回内容 不是 99999 则 单元测试不通过
                 .andExpect(MockMvcResultMatchers.content().string("99999"))
+                // 解析 JSON 值  判断内容
                 //.andExpect( MockMvcResultMatchers.jsonPath("$.data.code").value(200) )
+                //返回 请求体
                 .andReturn();
 
         System.out.println( mvcResult.getResponse().getContentAsString() );