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