From 29eb5c73f3b5db0fbd5f802f15f0c5a0eaa7a0c4 Mon Sep 17 00:00:00 2001 From: pendj Date: Thu, 9 Mar 2023 10:40:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(test):=E6=B7=BB=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=E5=9F=BA=E7=B1=BB=E5=92=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95Demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/java3y/austin/BaseTestController.java | 57 +++++++++++++++++++ .../austin/MiniProgramTestController.java | 26 +++++++++ 2 files changed, 83 insertions(+) create mode 100644 austin-web/src/test/java/com/java3y/austin/BaseTestController.java create mode 100644 austin-web/src/test/java/com/java3y/austin/MiniProgramTestController.java diff --git a/austin-web/src/test/java/com/java3y/austin/BaseTestController.java b/austin-web/src/test/java/com/java3y/austin/BaseTestController.java new file mode 100644 index 0000000..2b32186 --- /dev/null +++ b/austin-web/src/test/java/com/java3y/austin/BaseTestController.java @@ -0,0 +1,57 @@ +package com.java3y.austin; + +import cn.hutool.http.ContentType; +import cn.hutool.http.Header; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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.HttpHeaders; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.nio.charset.StandardCharsets; + +/** + * @description: 测试基类,主要用于前置处理和后置处理 + * + * @date: 2023/3/9 9:40 + * @author: pendj + */ +@Slf4j +@SpringBootTest +@AutoConfigureMockMvc +public class BaseTestController { + + @Autowired + protected MockMvc mvc; + + protected HttpHeaders headers; + protected ResultActions resultActions; + + @BeforeEach + public void beforeEach() { + headers = new HttpHeaders(); + headers.add(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue()); + } + + @AfterEach + public void afterEach() { + try { + MvcResult mvcResult = resultActions + .andDo(MockMvcResultHandlers.print()) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + + String content = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + log.info("response content: \n {}", content); + } catch (Exception e) { + log.error("error message: \n {}", e.getMessage()); + } + } +} \ No newline at end of file diff --git a/austin-web/src/test/java/com/java3y/austin/MiniProgramTestController.java b/austin-web/src/test/java/com/java3y/austin/MiniProgramTestController.java new file mode 100644 index 0000000..c306e12 --- /dev/null +++ b/austin-web/src/test/java/com/java3y/austin/MiniProgramTestController.java @@ -0,0 +1,26 @@ +package com.java3y.austin; + +import org.junit.jupiter.api.Test; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +public class MiniProgramTestController extends BaseTestController { + + private static final String BASE_URL = "/miniProgram"; + + /** + * 根据账号Id获取模板列表 + * + * @throws Exception + */ + @Test + public void queryList() throws Exception { + //doRequest + resultActions = mvc.perform( + MockMvcRequestBuilders + .get(BASE_URL + "/template/list") + .headers(headers) + .param("id","1") + ); + } + +} From 36b2441da12f50628263eed0c89626ae6adf1990 Mon Sep 17 00:00:00 2001 From: pendj Date: Thu, 9 Mar 2023 11:08:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat(test):=E6=B7=BB=E5=8A=A0Spring?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- austin-web/pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/austin-web/pom.xml b/austin-web/pom.xml index 2022d97..80453cd 100644 --- a/austin-web/pom.xml +++ b/austin-web/pom.xml @@ -16,6 +16,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-test + com.java3y.austin austin-handler