!47 feat(test):添加单元测试基类和测试Demo

Merge pull request !47 from pendj/master
pull/32/head
Java3y 2 years ago committed by Gitee
commit 75dbd98e06
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -16,6 +16,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.java3y.austin</groupId>
<artifactId>austin-handler</artifactId>

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

@ -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")
);
}
}
Loading…
Cancel
Save