添加测试支持基础项目
This commit is contained in:
parent
b7f4407089
commit
54a5c890f6
1
.gitignore
vendored
1
.gitignore
vendored
@ -36,5 +36,4 @@ application-local.yml
|
||||
*.log
|
||||
rebel.xml
|
||||
FeignConfigTest.java
|
||||
test/
|
||||
.DS_Store
|
||||
18
axzo-test-spring-boot-starter/README.md
Normal file
18
axzo-test-spring-boot-starter/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
## axzo-test-spring-boot-starter
|
||||
|
||||
单元测试支持库
|
||||
|
||||
## 使用方法
|
||||
|
||||
|
||||
## Service测试
|
||||
|
||||
|
||||
## Dao测试
|
||||
|
||||
|
||||
|
||||
## Controller
|
||||
|
||||
|
||||
## Http Rest Api测试
|
||||
69
axzo-test-spring-boot-starter/pom.xml
Normal file
69
axzo-test-spring-boot-starter/pom.xml
Normal file
@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.6</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<artifactId>axzo-test-spring-boot-starter</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>axzo-test-spring-boot-starter</name>
|
||||
<description>安心筑单元测试支持库</description>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<version>2.2.3.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.wix</groupId>
|
||||
<artifactId>wix-embedded-mysql</artifactId>
|
||||
<version>4.6.1</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-compress</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,36 @@
|
||||
package cn.axzo.framework.test;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration(exclude= {
|
||||
MetricsAutoConfiguration.class,
|
||||
UserDetailsServiceAutoConfiguration.class
|
||||
})
|
||||
public class ControllerTestConfiguration extends WebMvcConfigurationSupport{
|
||||
|
||||
/**
|
||||
* 增加HttpMessageConverter,设置输出数据编码为UTF-8
|
||||
*/
|
||||
@Bean
|
||||
public HttpMessageConverter<String> responseBodyConverter() {
|
||||
return new StringHttpMessageConverter(Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(responseBodyConverter());
|
||||
addDefaultHttpMessageConverters(converters);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,109 @@
|
||||
package cn.axzo.framework.test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@ActiveProfiles({ "dev" })
|
||||
@SpringBootTest(classes = ControllerTestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
|
||||
public abstract class ControllerTestSupport {
|
||||
|
||||
public static final String APPLICATION_JSON_UTF8 = "application/json;charset=UTF-8";
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
/**
|
||||
* MockMvc模拟对象
|
||||
*/
|
||||
protected MockMvc mockMvc;
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 单元测试运行前环境、参数初始化
|
||||
*/
|
||||
@BeforeEach
|
||||
protected void setUp() throws Exception {
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(getTestController()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取待测试Controller
|
||||
* @return
|
||||
*/
|
||||
protected abstract Object getTestController();
|
||||
|
||||
/**
|
||||
* 发起POST请求
|
||||
* @param url
|
||||
* @param arg
|
||||
* @return ResultActions
|
||||
* @throws Exception
|
||||
*/
|
||||
protected ResultActions doPostAction(String url,Object arg) throws Exception {
|
||||
String contentJson = mapper.writeValueAsString(arg);
|
||||
return mockMvc.perform(MockMvcRequestBuilders
|
||||
.post(url)
|
||||
.contentType(APPLICATION_JSON_UTF8)
|
||||
.accept(APPLICATION_JSON_UTF8)
|
||||
.content(contentJson))
|
||||
.andDo(MockMvcResultHandlers.print());
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起GET请求
|
||||
* @param url
|
||||
* @param arg
|
||||
* @return ResultActions
|
||||
* @throws Exception
|
||||
*/
|
||||
protected ResultActions doGetAction(String url,MultiValueMap<String,String> params) throws Exception {
|
||||
return mockMvc.perform(MockMvcRequestBuilders
|
||||
.get(url)
|
||||
.contentType(APPLICATION_JSON_UTF8)
|
||||
.accept(APPLICATION_JSON_UTF8)
|
||||
.params(params))
|
||||
.andDo(MockMvcResultHandlers.print());
|
||||
}
|
||||
/**
|
||||
* 发起GET请求
|
||||
* @param url
|
||||
* @param arg
|
||||
* @return ResultActions
|
||||
* @throws Exception
|
||||
*/
|
||||
protected ResultActions doGetAction(String url,Map<String,String> params) throws Exception {
|
||||
MultiValueMap<String,String> multiValueMap = new LinkedMultiValueMap<>();
|
||||
params.forEach((k,v)->{
|
||||
multiValueMap.add(k, v);
|
||||
});
|
||||
|
||||
return mockMvc.perform(MockMvcRequestBuilders
|
||||
.get(url)
|
||||
.contentType(APPLICATION_JSON_UTF8)
|
||||
.accept(APPLICATION_JSON_UTF8)
|
||||
.params(multiValueMap))
|
||||
.andDo(MockMvcResultHandlers.print());
|
||||
}
|
||||
|
||||
/**
|
||||
* 单元测试运行后脏数据清理,对象关闭
|
||||
*/
|
||||
@AfterEach
|
||||
protected void tearDown() throws Exception {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package cn.axzo.framework.test;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration(exclude= {
|
||||
MetricsAutoConfiguration.class
|
||||
})
|
||||
public class ServiceTestConfiguration {
|
||||
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package cn.axzo.framework.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
@ActiveProfiles({"dev"})
|
||||
@SpringBootTest(classes = ServiceTestConfiguration.class,webEnvironment = WebEnvironment.MOCK)
|
||||
public class ServiceTestSupport {
|
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@AfterEach
|
||||
void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
package cn.axzo.framework.test.demo;
|
||||
|
||||
public class DemoComtroller {
|
||||
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
package cn.axzo.framework.test.demo;
|
||||
|
||||
public class DemoServic {
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package cn.axzo.framework.test.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
import cn.axzo.framework.test.ServiceTestSupport;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = "cn.axzo")
|
||||
@EnableFeignClients(basePackages = "cn.axzo")
|
||||
public class DemoServiceTest extends ServiceTestSupport{
|
||||
|
||||
|
||||
@Test
|
||||
void testDemo() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package cn.axzo.framework.test.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import cn.axzo.framework.test.ControllerTestSupport;
|
||||
|
||||
|
||||
@SpringBootApplication(scanBasePackages = "cn.axzo.framework.test.demo")
|
||||
@EnableFeignClients(basePackages = "cn.axzo.framework.test.demo")
|
||||
class DemoComtrollerTest extends ControllerTestSupport{
|
||||
|
||||
@Autowired
|
||||
DemoComtroller api;
|
||||
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return api;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDemo() throws Exception {
|
||||
String url = "/api/workspace/queryWorkspace";
|
||||
//设置请求参数
|
||||
Object query = new Object();
|
||||
doPostAction(url, query)
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andReturn();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
spring:
|
||||
application:
|
||||
name: unitest
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: ${NACOS_HOST:dev-nacos.axzo.cn}:${NACOS_PORT:80}
|
||||
# server-addr: ${NACOS_HOST:test-nacos.axzo.cn}:${NACOS_PORT:80}
|
||||
# server-addr: ${NACOS_HOST:test1-nacos.axzo.cn}:${NACOS_PORT:80}
|
||||
file-extension: yaml
|
||||
namespace: ${NACOS_NAMESPACE_ID:35eada10-9574-4db8-9fea-bc6a4960b6c7}
|
||||
# namespace: ${NACOS_NAMESPACE_ID:f3c0f0d2-bac4-4498-bee7-9c3636b3afdf}
|
||||
# namespace: ${NACOS_NAMESPACE_ID:6b278234-1409-4054-beb7-4bbc0def8e54}
|
||||
prefix: ${spring.application.name}
|
||||
profiles:
|
||||
active: ${NACOS_PROFILES_ACTIVE:dev}
|
||||
# active: ${NACOS_PROFILES_ACTIVE:test}
|
||||
# active: ${NACOS_PROFILES_ACTIVE:test1}
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
logging:
|
||||
level:
|
||||
com.alibaba.nacos.client.config.impl: WARN
|
||||
mybatis-plus:
|
||||
type-enums-package: cn.axzo.apollo.server.enums,cn.axzo.apollo.core.enums,cn.axzo.apollo.order.enums
|
||||
@ -0,0 +1,57 @@
|
||||
<configuration>
|
||||
<!-- 引用 Spring Boot 的 logback 基础配置 -->
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
|
||||
<!-- 变量 yudao.info.base-package,基础业务包 -->
|
||||
<springProperty scope="context" name="appName" source="spring.application.name"/>
|
||||
<!-- 格式化输出:%d 表示日期,%X{tid} SkWalking 链路追踪编号,%thread 表示线程名,%-5level:级别从左显示 5 个字符宽度,%msg:日志消息,%n是换行符 -->
|
||||
<property name="PATTERN_DEFAULT" value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%thread] [%tid] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
|
||||
|
||||
<property name="LOG_FILE" value="target/${appName}.log"/>
|
||||
|
||||
<!-- 控制台 Appender -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
|
||||
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
|
||||
<pattern>${PATTERN_DEFAULT}</pattern>
|
||||
</layout>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 文件 Appender -->
|
||||
<!-- 参考 Spring Boot 的 file-appender.xml 编写 -->
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
|
||||
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
|
||||
<pattern>${PATTERN_DEFAULT}</pattern>
|
||||
</layout>
|
||||
</encoder>
|
||||
<!-- 日志文件名 -->
|
||||
<file>${LOG_FILE}</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<!-- 滚动后的日志文件名 -->
|
||||
<fileNamePattern>${LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
|
||||
<!-- 启动服务时,是否清理历史日志,一般不建议清理 -->
|
||||
<cleanHistoryOnStart>${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
|
||||
<!-- 日志文件,到达多少容量,进行滚动 -->
|
||||
<maxFileSize>${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB}</maxFileSize>
|
||||
<!-- 日志文件的总大小,0 表示不限制 -->
|
||||
<totalSizeCap>${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0}</totalSizeCap>
|
||||
<!-- 日志文件的保留天数 -->
|
||||
<maxHistory>${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-30}</maxHistory>
|
||||
</rollingPolicy>
|
||||
</appender>
|
||||
|
||||
<!-- 本地环境 -->
|
||||
<springProfile name="local,dev,test,unitest">
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</springProfile>
|
||||
<!-- 其它环境 -->
|
||||
<springProfile name="dev,test,test1,pre,master,default">
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</springProfile>
|
||||
|
||||
</configuration>
|
||||
Loading…
Reference in New Issue
Block a user