wuheng 2 jaren geleden
bovenliggende
commit
d41ff2f83c
2 gewijzigde bestanden met toevoegingen van 86 en 1 verwijderingen
  1. 8 1
      demo/pom.xml
  2. 78 0
      demo/src/test/java/com/lovecoding/springboot/demo/demo/DemoControllerTest.java

+ 8 - 1
demo/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
-        <version>2.7.10</version>
+        <version>2.7.9</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
     <groupId>com.lovecoding.springboot.demo</groupId>
@@ -44,6 +44,13 @@
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-webflux</artifactId>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 78 - 0
demo/src/test/java/com/lovecoding/springboot/demo/demo/DemoControllerTest.java

@@ -0,0 +1,78 @@
+package com.lovecoding.springboot.demo.demo;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.reactive.server.WebTestClient;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+
+import javax.annotation.Resource;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.when;
+
+
+/**
+ * 单元测试 指的时 我们开发过程当中对我们自己的 功能点进行测试
+ * 集成测试 指的的 把相关功能点串联 测试
+ */
+
+//@SpringBootTest
+//@AutoConfigureMockMvc
+//@WebMvcTest(DemoController.class)
+@WebFluxTest(DemoController.class)
+class DemoControllerTest {
+
+    //@Resource
+    //MockMvc mockMvc;
+
+    @Resource
+    WebTestClient webClient;
+
+    @MockBean
+    TestMapper testMapper;
+
+
+
+    @Test
+    public void webTest(){
+
+        webClient.get().uri("/")
+                .accept(MediaType.APPLICATION_JSON_UTF8)
+                .exchange().expectStatus().isOk();
+
+    }
+
+    @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())
+                .andExpect(MockMvcResultMatchers.status().is(200))
+                .andExpect(MockMvcResultMatchers.content().string("99999"))
+                //.andExpect( MockMvcResultMatchers.jsonPath("$.data.code").value(200) )
+                .andReturn();
+
+        System.out.println( mvcResult.getResponse().getContentAsString() );
+
+    }
+
+}