+BaseService相关接口的单元测试

This commit is contained in:
mazhicheng 2019-06-15 15:40:54 +08:00
parent cc84791126
commit 136e83d33a
5 changed files with 219 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package diboot.core.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
* @author Administrator
*/
@SpringBootApplication
public class StartupApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(StartupApplication.class, args);
}
}

View File

@ -0,0 +1,76 @@
package diboot.core.test.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.diboot.core.util.D;
import com.diboot.core.util.DateConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
/***
* Spring配置文件
* @author Mazhicheng
* @version v2.0
* @date 2019/6/10
*/
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement(proxyTargetClass=true)
@ComponentScan(basePackages={"com.diboot"})
@MapperScan({"com.diboot.**.mapper"})
public class SpringMvcConfig implements WebMvcConfigurer{
private static final Logger log = LoggerFactory.getLogger(SpringMvcConfig.class);
/**
* JSON转换组件替换为fastJson
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(fastMediaTypes);
// 配置转换格式
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 设置fastjson的序列化参数禁用循环依赖检测数据兼容浏览器端避免JS端Long精度丢失问题
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.BrowserCompatible);
fastJsonConfig.setDateFormat(D.FORMAT_DATETIME_Y4MDHM);
converter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> httpMsgConverter = converter;
return new HttpMessageConverters(httpMsgConverter);
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter());
}
/**
* Mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}

View File

@ -0,0 +1,89 @@
package diboot.core.test.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diboot.core.config.BaseConfig;
import com.diboot.core.entity.Metadata;
import com.diboot.core.service.MetadataService;
import com.diboot.core.util.V;
import com.diboot.core.vo.Pagination;
import diboot.core.test.StartupApplication;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/**
* BaseService接口实现测试 (需先执行example中的初始化SQL)
* @author Mazhicheng
* @version v2.0
* @date 2019/06/15
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {StartupApplication.class})
public class BaseServiceTest {
@Autowired
MetadataService metadataService;
@Test
public void testGet(){
// 查询总数
int count = metadataService.getEntityListCount(null);
Assert.assertTrue(count > 0);
// 查询list
List<Metadata> metadataList = metadataService.getEntityList(null);
Assert.assertTrue(V.notEmpty(metadataList));
Assert.assertTrue(metadataList.size() == count);
// 第一页数据
List<Metadata> pageList = metadataService.getEntityList(null, new Pagination());
Assert.assertTrue(pageList.size() > 0 && pageList.size() <= BaseConfig.getPageSize());
// 查询单个记录
Long id = metadataList.get(0).getId();
Metadata first = metadataService.getEntity(id);
Assert.assertTrue(first != null);
// 只查询第一条记录对应type类型的
LambdaQueryWrapper<Metadata> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Metadata::getType, first.getType());
metadataList = metadataService.getEntityList(queryWrapper);
Assert.assertTrue(V.notEmpty(metadataList));
// 结果type值一致
metadataList.stream().forEach( m -> {
Assert.assertTrue(m.getType().equals(first.getType()));
});
}
@Test
public void testCreateUpdateAndDelete(){
// 创建
String TYPE = "ID_TYPE";
Metadata metadata = new Metadata();
metadata.setType(TYPE);
metadata.setItemName("证件品类");
metadata.setParentId(0L);
metadataService.createEntity(metadata);
// 查询是否创建成功
LambdaQueryWrapper<Metadata> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Metadata::getType, TYPE);
List<Metadata> metadataList = metadataService.getEntityList(queryWrapper);
Assert.assertTrue(V.notEmpty(metadataList));
// 更新
metadata.setItemName("证件类型");
metadataService.updateEntity(metadata);
Metadata metadata2 = metadataService.getEntity(metadata.getId());
Assert.assertTrue(metadata2.getItemName().equals(metadata.getItemName()));
// 删除
metadataService.deleteEntity(metadata.getId());
metadata2 = metadataService.getEntity(metadata.getId());
Assert.assertTrue(metadata2 == null);
}
}

View File

@ -0,0 +1,32 @@
# spring config
spring.devtools.restart.enabled=true
#datasource config
spring.datasource.url=jdbc:mysql://localhost:3306/diboot_example?characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=diboot
spring.datasource.password=123456
spring.datasource.hikari.maximum-pool-size=5
spring.datasource.hikari.data-source-properties.useInformationSchema=true
spring.datasource.hikari.data-source-properties.nullCatalogMeansCurrent=true
# 数据库驱动
spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
# mybatis配置
#mybatis.configuration.cache-enabled=false
#mybatis.configuration.lazy-loading-enabled=true
#mybatis.configuration.map-underscore-to-camel-case=true
#mybatis.configuration.multiple-result-sets-enabled=false
#mybatis.configuration.use-generated-keys=true
#mybatis.configuration.auto-mapping-behavior=full
#mybatis.configuration.default-statement-timeout=60
#mybatis.configuration.log-impl=org.apache.ibatis.logging.log4j2.Log4j2Impl
# logging config
logging.pattern.console=%clr{%d{MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${PID}}{faint} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx
logging.level.root=info
logging.level.org.apache=info
logging.level.org.hibernate.validator=info
logging.level.org.springframework=info
logging.level.com.zaxxer.hikari=info
logging.level.com.diboot=debug
logging.level.org.mybatis=debug

View File

@ -0,0 +1,5 @@
__ _ __ __
____/ / (_) / /_ ____ ____ / /_
/ __ / / / / __ \ / __ \ / __ \ / __/
/ /_/ / / / / /_/ / / /_/ / / /_/ / / /_
\__,_/ /_/ /_.___/ \____/ \____/ \__/