添加单元测试包
This commit is contained in:
@@ -29,6 +29,9 @@
|
|||||||
<redisson.version>3.18.0</redisson.version>
|
<redisson.version>3.18.0</redisson.version>
|
||||||
<spring-boot-admin.version>2.7.10</spring-boot-admin.version>
|
<spring-boot-admin.version>2.7.10</spring-boot-admin.version>
|
||||||
<transmittable-thread-local.version>2.14.2</transmittable-thread-local.version>
|
<transmittable-thread-local.version>2.14.2</transmittable-thread-local.version>
|
||||||
|
<mockito-inline.version>4.11.0</mockito-inline.version>
|
||||||
|
<jedis-mock.version>1.0.7</jedis-mock.version>
|
||||||
|
<podam.version>7.2.11.RELEASE</podam.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@@ -125,6 +128,11 @@
|
|||||||
<artifactId>orion-ops-spring-boot-starter-monitor</artifactId>
|
<artifactId>orion-ops-spring-boot-starter-monitor</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.orion.ops</groupId>
|
||||||
|
<artifactId>orion-ops-spring-boot-starter-test</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- websocket -->
|
<!-- websocket -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -241,6 +249,47 @@
|
|||||||
<version>${transmittable-thread-local.version}</version>
|
<version>${transmittable-thread-local.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>asm</artifactId>
|
||||||
|
<groupId>org.ow2.asm</groupId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>com.vaadin.external.google</groupId>
|
||||||
|
<artifactId>android-json</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test mockito -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-inline</artifactId>
|
||||||
|
<version>${mockito-inline.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test redis -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.fppt</groupId>
|
||||||
|
<artifactId>jedis-mock</artifactId>
|
||||||
|
<version>${jedis-mock.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test entity 随机生成器 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>uk.co.jemos.podam</groupId>
|
||||||
|
<artifactId>podam</artifactId>
|
||||||
|
<version>${podam.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|||||||
@AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_DATASOURCE)
|
@AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_DATASOURCE)
|
||||||
@EnableTransactionManagement(proxyTargetClass = true)
|
@EnableTransactionManagement(proxyTargetClass = true)
|
||||||
@EnableConfigurationProperties(DruidStatProperties.class)
|
@EnableConfigurationProperties(DruidStatProperties.class)
|
||||||
public class OrionDatasourceAutoConfiguration {
|
public class OrionDataSourceAutoConfiguration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param properties 配置
|
* @param properties 配置
|
||||||
@@ -1 +1 @@
|
|||||||
com.orion.ops.framework.datasource.config.OrionDatasourceAutoConfiguration
|
com.orion.ops.framework.datasource.config.OrionDataSourceAutoConfiguration
|
||||||
@@ -12,6 +12,7 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ public class OrionMybatisAutoConfiguration {
|
|||||||
* @return 字段填充元数据处理器
|
* @return 字段填充元数据处理器
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
|
@ConditionalOnBean(SecurityHolder.class)
|
||||||
public MetaObjectHandler defaultMetaObjectHandler(SecurityHolder securityHolder) {
|
public MetaObjectHandler defaultMetaObjectHandler(SecurityHolder securityHolder) {
|
||||||
// 设置填充工具参数
|
// 设置填充工具参数
|
||||||
DomainFillUtils.setSecurityHolder(securityHolder);
|
DomainFillUtils.setSecurityHolder(securityHolder);
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<groupId>com.orion.ops</groupId>
|
||||||
|
<artifactId>orion-ops-framework</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>orion-ops-spring-boot-starter-test</artifactId>
|
||||||
|
<name>${project.artifactId}</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<description>项目单元测试包</description>
|
||||||
|
<url>https://github.com/lijiahangmax/orion-ops-pro</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.orion.ops</groupId>
|
||||||
|
<artifactId>orion-ops-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.orion.ops</groupId>
|
||||||
|
<artifactId>orion-ops-spring-boot-starter-mybatis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.orion.ops</groupId>
|
||||||
|
<artifactId>orion-ops-spring-boot-starter-datasource</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test mockito -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-inline</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test h2 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test redis -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.fppt</groupId>
|
||||||
|
<artifactId>jedis-mock</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- test entity 随机生成器 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>uk.co.jemos.podam</groupId>
|
||||||
|
<artifactId>podam</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.orion.ops.framework.test.config;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||||
|
import org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元测试 H2 数据库 DML 初始化脚本
|
||||||
|
*
|
||||||
|
* @author Jiahang Li
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2023/8/23 17:17
|
||||||
|
*/
|
||||||
|
@Profile("unit-test")
|
||||||
|
@Lazy(value = false)
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnMissingBean(AbstractScriptDatabaseInitializer.class)
|
||||||
|
@ConditionalOnSingleCandidate(DataSource.class)
|
||||||
|
@ConditionalOnClass(name = "org.springframework.jdbc.datasource.init.DatabasePopulator")
|
||||||
|
@EnableConfigurationProperties(SqlInitializationProperties.class)
|
||||||
|
public class OrionH2SqlInitializationTestConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据源脚本初始化 Bean
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public DataSourceScriptDatabaseInitializer dataSourceScriptDatabaseInitializer(DataSource dataSource,
|
||||||
|
SqlInitializationProperties initializationProperties) {
|
||||||
|
// TODO 看看正常情况下会不会有
|
||||||
|
// 初始化配置
|
||||||
|
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
|
||||||
|
settings.setSchemaLocations(initializationProperties.getSchemaLocations());
|
||||||
|
settings.setDataLocations(initializationProperties.getDataLocations());
|
||||||
|
settings.setContinueOnError(initializationProperties.isContinueOnError());
|
||||||
|
settings.setSeparator(initializationProperties.getSeparator());
|
||||||
|
settings.setEncoding(initializationProperties.getEncoding());
|
||||||
|
settings.setMode(initializationProperties.getMode());
|
||||||
|
// 初始化
|
||||||
|
return new DataSourceScriptDatabaseInitializer(dataSource, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.orion.ops.framework.test.config;
|
||||||
|
|
||||||
|
import com.github.fppt.jedismock.RedisServer;
|
||||||
|
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元测试 redis 初始化
|
||||||
|
*
|
||||||
|
* @author Jiahang Li
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2023/8/23 17:19
|
||||||
|
*/
|
||||||
|
@Lazy(false)
|
||||||
|
@Profile("unit-test")
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@EnableConfigurationProperties(RedisProperties.class)
|
||||||
|
public class OrionMockRedisTestConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mockRedisServer
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public RedisServer redisServer(RedisProperties properties) {
|
||||||
|
// TODO 看看正常情况下会不会有
|
||||||
|
RedisServer redisServer = new RedisServer(properties.getPort());
|
||||||
|
try {
|
||||||
|
redisServer.start();
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
}
|
||||||
|
return redisServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package com.orion.ops.framework.test.core.base;
|
||||||
|
|
||||||
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
|
||||||
|
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
|
||||||
|
import com.orion.ops.framework.datasource.config.OrionDataSourceAutoConfiguration;
|
||||||
|
import com.orion.ops.framework.mybatis.config.OrionMybatisAutoConfiguration;
|
||||||
|
import com.orion.ops.framework.test.config.OrionH2SqlInitializationTestConfiguration;
|
||||||
|
import com.orion.ops.framework.test.config.OrionMockRedisTestConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.annotation.Rollback;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.jdbc.Sql;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元测试父类
|
||||||
|
*
|
||||||
|
* @author Jiahang Li
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2023/8/23 15:12
|
||||||
|
*/
|
||||||
|
@Rollback
|
||||||
|
@ActiveProfiles("unit-test")
|
||||||
|
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseUnitTest.Application.class)
|
||||||
|
public class BaseUnitTest {
|
||||||
|
|
||||||
|
@Import({
|
||||||
|
// datasource
|
||||||
|
OrionDataSourceAutoConfiguration.class,
|
||||||
|
DataSourceAutoConfiguration.class,
|
||||||
|
DataSourceTransactionManagerAutoConfiguration.class,
|
||||||
|
OrionH2SqlInitializationTestConfiguration.class,
|
||||||
|
DruidDataSourceAutoConfigure.class,
|
||||||
|
// mybatis
|
||||||
|
OrionMybatisAutoConfiguration.class,
|
||||||
|
MybatisPlusAutoConfiguration.class,
|
||||||
|
// redis
|
||||||
|
OrionMockRedisTestConfiguration.class,
|
||||||
|
})
|
||||||
|
public static class Application {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package com.orion.ops.framework.test.core.utils;
|
||||||
|
|
||||||
|
import com.orion.lang.utils.Arrays1;
|
||||||
|
import com.orion.lang.utils.random.Randoms;
|
||||||
|
import uk.co.jemos.podam.api.PodamFactory;
|
||||||
|
import uk.co.jemos.podam.api.PodamFactoryImpl;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对象生成器
|
||||||
|
*
|
||||||
|
* @author Jiahang Li
|
||||||
|
* @version 1.0.0
|
||||||
|
* @since 2023/8/23 14:13
|
||||||
|
*/
|
||||||
|
public class EntityRandoms {
|
||||||
|
|
||||||
|
private EntityRandoms() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final int RANDOM_STRING_LENGTH = 5;
|
||||||
|
|
||||||
|
private static final int RANDOM_INT_MAX = 10;
|
||||||
|
|
||||||
|
private static final int RANDOM_COLLECTION_LENGTH = 5;
|
||||||
|
|
||||||
|
private static final String DELETED = "deleted";
|
||||||
|
|
||||||
|
private static final PodamFactory FACTORY = new PodamFactoryImpl();
|
||||||
|
|
||||||
|
static {
|
||||||
|
// 字符串
|
||||||
|
FACTORY.getStrategy().addOrReplaceTypeManufacturer(String.class, (dataProviderStrategy, attributeMetadata, map) -> Randoms.randomLetter(RANDOM_STRING_LENGTH));
|
||||||
|
// Integer
|
||||||
|
FACTORY.getStrategy().addOrReplaceTypeManufacturer(Integer.class, (dataProviderStrategy, attributeMetadata, map) -> Randoms.randomInt(0, RANDOM_INT_MAX));
|
||||||
|
// Boolean
|
||||||
|
FACTORY.getStrategy().addOrReplaceTypeManufacturer(Boolean.class, (dataProviderStrategy, attributeMetadata, map) -> {
|
||||||
|
if (DELETED.equals(attributeMetadata.getAttributeName())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Randoms.randomBoolean();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public static <T> T random(Class<T> clazz, Consumer<T>... consumers) {
|
||||||
|
T e = FACTORY.manufacturePojo(clazz);
|
||||||
|
if (Arrays1.isNotEmpty(consumers)) {
|
||||||
|
Arrays.stream(consumers).forEach(consumer -> consumer.accept(e));
|
||||||
|
}
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public static <T> T random(Class<T> clazz, Type type, Consumer<T>... consumers) {
|
||||||
|
T e = FACTORY.manufacturePojo(clazz, type);
|
||||||
|
if (Arrays1.isNotEmpty(consumers)) {
|
||||||
|
Arrays.stream(consumers).forEach(consumer -> consumer.accept(e));
|
||||||
|
}
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public static <T> Set<T> randomSet(Class<T> clazz, Consumer<T>... consumers) {
|
||||||
|
return Stream.iterate(0, i -> i)
|
||||||
|
.limit(Randoms.randomInt(1, RANDOM_COLLECTION_LENGTH))
|
||||||
|
.map(o -> random(clazz, consumers))
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public static <T> List<T> randomList(Class<T> clazz, Consumer<T>... consumers) {
|
||||||
|
return Stream.iterate(0, i -> i)
|
||||||
|
.limit(Randoms.randomInt(1, RANDOM_COLLECTION_LENGTH))
|
||||||
|
.map(o -> random(clazz, consumers))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
<module>orion-ops-spring-boot-starter-storage</module>
|
<module>orion-ops-spring-boot-starter-storage</module>
|
||||||
<module>orion-ops-spring-boot-starter-security</module>
|
<module>orion-ops-spring-boot-starter-security</module>
|
||||||
<module>orion-ops-spring-boot-starter-monitor</module>
|
<module>orion-ops-spring-boot-starter-monitor</module>
|
||||||
|
<module>orion-ops-spring-boot-starter-test</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
Reference in New Issue
Block a user