|
@@ -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() );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|