Commit adac2e63cb3b7ba243348a624e936a2b95194679
Merge branch 'rouyi/master' into feature/notice_test
Showing
16 changed files
with
182 additions
and
87 deletions
src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java
| ... | ... | @@ -13,7 +13,8 @@ import org.springframework.context.annotation.Configuration; |
| 13 | 13 | * @author 芋道源码 |
| 14 | 14 | */ |
| 15 | 15 | @Configuration |
| 16 | -@MapperScan(value = "${yudao.info.base-package}", annotationClass = Mapper.class) | |
| 16 | +@MapperScan(value = "${yudao.info.base-package}", annotationClass = Mapper.class, | |
| 17 | + lazyInitialization = "${mybatis.lazy-initialization:false}") // Mapper 懒加载,目前仅用于单元测试 | |
| 17 | 18 | public class MybatisConfiguration { |
| 18 | 19 | |
| 19 | 20 | @Bean | ... | ... |
src/main/java/cn/iocoder/dashboard/framework/security/config/SecurityConfiguration.java
| ... | ... | @@ -152,7 +152,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { |
| 152 | 152 | .anyRequest().authenticated() |
| 153 | 153 | .and() |
| 154 | 154 | .headers().frameOptions().disable(); |
| 155 | - httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); | |
| 155 | + httpSecurity.logout().logoutUrl(webProperties.getApiPrefix() + "/logout").logoutSuccessHandler(logoutSuccessHandler); | |
| 156 | 156 | // 添加 JWT Filter |
| 157 | 157 | httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); |
| 158 | 158 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/framework/security/core/handler/LogoutSuccessHandlerImpl.java
| 1 | 1 | package cn.iocoder.dashboard.framework.security.core.handler; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | +import cn.iocoder.dashboard.common.pojo.CommonResult; | |
| 4 | 5 | import cn.iocoder.dashboard.framework.security.config.SecurityProperties; |
| 5 | 6 | import cn.iocoder.dashboard.framework.security.core.service.SecurityAuthFrameworkService; |
| 6 | 7 | import cn.iocoder.dashboard.framework.security.core.util.SecurityFrameworkUtils; |
| ... | ... | @@ -36,6 +37,6 @@ public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler { |
| 36 | 37 | securityFrameworkService.logout(token); |
| 37 | 38 | } |
| 38 | 39 | // 返回成功 |
| 39 | - ServletUtils.writeJSON(response, null); | |
| 40 | + ServletUtils.writeJSON(response, CommonResult.success(null)); | |
| 40 | 41 | } |
| 41 | 42 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/framework/security/core/util/SecurityFrameworkUtils.java
| ... | ... | @@ -2,7 +2,10 @@ package cn.iocoder.dashboard.framework.security.core.util; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.dashboard.framework.security.core.LoginUser; |
| 4 | 4 | import cn.iocoder.dashboard.framework.web.core.util.WebFrameworkUtils; |
| 5 | +import org.springframework.lang.Nullable; | |
| 5 | 6 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 7 | +import org.springframework.security.core.Authentication; | |
| 8 | +import org.springframework.security.core.context.SecurityContext; | |
| 6 | 9 | import org.springframework.security.core.context.SecurityContextHolder; |
| 7 | 10 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
| 8 | 11 | import org.springframework.util.StringUtils; |
| ... | ... | @@ -40,9 +43,20 @@ public class SecurityFrameworkUtils { |
| 40 | 43 | |
| 41 | 44 | /** |
| 42 | 45 | * 获取当前用户 |
| 46 | + * | |
| 47 | + * @return 当前用户 | |
| 43 | 48 | */ |
| 49 | + @Nullable | |
| 44 | 50 | public static LoginUser getLoginUser() { |
| 45 | - return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |
| 51 | + SecurityContext context = SecurityContextHolder.getContext(); | |
| 52 | + if (context == null) { | |
| 53 | + return null; | |
| 54 | + } | |
| 55 | + Authentication authentication = context.getAuthentication(); | |
| 56 | + if (authentication == null) { | |
| 57 | + return null; | |
| 58 | + } | |
| 59 | + return (LoginUser) authentication.getPrincipal(); | |
| 46 | 60 | } |
| 47 | 61 | |
| 48 | 62 | /** |
| ... | ... | @@ -50,8 +64,10 @@ public class SecurityFrameworkUtils { |
| 50 | 64 | * |
| 51 | 65 | * @return 用户编号 |
| 52 | 66 | */ |
| 67 | + @Nullable | |
| 53 | 68 | public static Long getLoginUserId() { |
| 54 | - return getLoginUser().getId(); | |
| 69 | + LoginUser loginUser = getLoginUser(); | |
| 70 | + return loginUser != null ? loginUser.getId() : null; | |
| 55 | 71 | } |
| 56 | 72 | |
| 57 | 73 | /** |
| ... | ... | @@ -59,8 +75,10 @@ public class SecurityFrameworkUtils { |
| 59 | 75 | * |
| 60 | 76 | * @return 角色编号数组 |
| 61 | 77 | */ |
| 78 | + @Nullable | |
| 62 | 79 | public static Set<Long> getLoginUserRoleIds() { |
| 63 | - return getLoginUser().getRoleIds(); | |
| 80 | + LoginUser loginUser = getLoginUser(); | |
| 81 | + return loginUser != null ? loginUser.getRoleIds() : null; | |
| 64 | 82 | } |
| 65 | 83 | |
| 66 | 84 | /** | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java
| ... | ... | @@ -160,7 +160,26 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 160 | 160 | |
| 161 | 161 | @Override |
| 162 | 162 | public void logout(String token) { |
| 163 | -// AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功")); TODO 需要搞一搞 | |
| 163 | + // 查询用户信息 | |
| 164 | + LoginUser loginUser = userSessionService.getLoginUser(token); | |
| 165 | + if (loginUser == null) { | |
| 166 | + return; | |
| 167 | + } | |
| 168 | + // 删除 session | |
| 169 | + userSessionService.deleteUserSession(token); | |
| 170 | + // 记录登出日子和 | |
| 171 | + this.createLogoutLog(loginUser.getUsername()); | |
| 172 | + } | |
| 173 | + | |
| 174 | + private void createLogoutLog(String username) { | |
| 175 | + SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO(); | |
| 176 | + reqVO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); | |
| 177 | + reqVO.setTraceId(TracerUtils.getTraceId()); | |
| 178 | + reqVO.setUsername(username); | |
| 179 | + reqVO.setUserAgent(ServletUtils.getUserAgent()); | |
| 180 | + reqVO.setUserIp(ServletUtils.getClientIP()); | |
| 181 | + reqVO.setResult(SysLoginResultEnum.SUCCESS.getResult()); | |
| 182 | + loginLogService.createLoginLog(reqVO); | |
| 164 | 183 | } |
| 165 | 184 | |
| 166 | 185 | @Override | ... | ... |
src/test/java/cn/iocoder/dashboard/BaseDbAndRedisUnitTest.java
0 → 100644
| 1 | +package cn.iocoder.dashboard; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.config.RedisTestConfiguration; | |
| 4 | +import cn.iocoder.dashboard.framework.datasource.config.DataSourceConfiguration; | |
| 5 | +import cn.iocoder.dashboard.framework.mybatis.config.MybatisConfiguration; | |
| 6 | +import cn.iocoder.dashboard.framework.redis.config.RedisConfig; | |
| 7 | +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; | |
| 8 | +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; | |
| 9 | +import org.redisson.spring.starter.RedissonAutoConfiguration; | |
| 10 | +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; | |
| 11 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; | |
| 13 | +import org.springframework.context.annotation.Import; | |
| 14 | +import org.springframework.test.context.ActiveProfiles; | |
| 15 | +import org.springframework.test.context.jdbc.Sql; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * 依赖内存 DB 的单元测试 | |
| 19 | + * | |
| 20 | + * 注意,Service 层同样适用。对于 Service 层的单元测试,我们针对自己模块的 Mapper 走的是 H2 内存数据库,针对别的模块的 Service 走的是 Mock 方法 | |
| 21 | + * | |
| 22 | + * @author 芋道源码 | |
| 23 | + */ | |
| 24 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbAndRedisUnitTest.Application.class) | |
| 25 | +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 | |
| 26 | +@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB | |
| 27 | +public class BaseDbAndRedisUnitTest { | |
| 28 | + | |
| 29 | + @Import({ | |
| 30 | + // DB 配置类 | |
| 31 | + DataSourceConfiguration.class, // 自己的 DB 配置类 | |
| 32 | + DataSourceAutoConfiguration.class, // Spring DB 自动配置类 | |
| 33 | + DruidDataSourceAutoConfigure.class, // Druid 自动配置类 | |
| 34 | + // MyBatis 配置类 | |
| 35 | + MybatisConfiguration.class, // 自己的 MyBatis 配置类 | |
| 36 | + MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | |
| 37 | + // Redis 配置类 | |
| 38 | + RedisTestConfiguration.class, // Redis 测试配置类,用于启动 RedisServer | |
| 39 | + RedisAutoConfiguration.class, // Spring Redis 自动配置类 | |
| 40 | + RedisConfig.class, // 自己的 Redis 配置类 | |
| 41 | + RedissonAutoConfiguration.class, // Redisson 自动高配置类 | |
| 42 | + }) | |
| 43 | + public static class Application { | |
| 44 | + } | |
| 45 | + | |
| 46 | +} | ... | ... |
src/test/java/cn/iocoder/dashboard/BaseDbUnitTest.java
0 → 100644
| 1 | +package cn.iocoder.dashboard; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.framework.datasource.config.DataSourceConfiguration; | |
| 4 | +import cn.iocoder.dashboard.framework.mybatis.config.MybatisConfiguration; | |
| 5 | +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; | |
| 6 | +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; | |
| 7 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; | |
| 9 | +import org.springframework.context.annotation.Import; | |
| 10 | +import org.springframework.test.context.ActiveProfiles; | |
| 11 | +import org.springframework.test.context.jdbc.Sql; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * 依赖内存 DB 的单元测试 | |
| 15 | + * | |
| 16 | + * 注意,Service 层同样适用。对于 Service 层的单元测试,我们针对自己模块的 Mapper 走的是 H2 内存数据库,针对别的模块的 Service 走的是 Mock 方法 | |
| 17 | + * | |
| 18 | + * @author 芋道源码 | |
| 19 | + */ | |
| 20 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbUnitTest.Application.class) | |
| 21 | +@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 | |
| 22 | +@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB | |
| 23 | +public class BaseDbUnitTest { | |
| 24 | + | |
| 25 | + @Import({ | |
| 26 | + // DB 配置类 | |
| 27 | + DataSourceConfiguration.class, // 自己的 DB 配置类 | |
| 28 | + DataSourceAutoConfiguration.class, // Spring DB 自动配置类 | |
| 29 | + DruidDataSourceAutoConfigure.class, // Druid 自动配置类 | |
| 30 | + // MyBatis 配置类 | |
| 31 | + MybatisConfiguration.class, // 自己的 MyBatis 配置类 | |
| 32 | + MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 | |
| 33 | + }) | |
| 34 | + public static class Application { | |
| 35 | + } | |
| 36 | + | |
| 37 | +} | ... | ... |
src/test/java/cn/iocoder/dashboard/BaseSpringBootUnitTest.java
| ... | ... | @@ -12,6 +12,7 @@ import javax.annotation.Resource; |
| 12 | 12 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) |
| 13 | 13 | @ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件 |
| 14 | 14 | @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) // 每个单元测试结束后,清理 DB |
| 15 | +@Deprecated | |
| 15 | 16 | public class BaseSpringBootUnitTest { |
| 16 | 17 | |
| 17 | 18 | @Resource | ... | ... |
src/test/java/cn/iocoder/dashboard/config/RedisTestConfiguration.java
| ... | ... | @@ -8,12 +8,10 @@ import org.springframework.boot.autoconfigure.data.redis.RedisProperties; |
| 8 | 8 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 9 | 9 | import org.springframework.context.annotation.Bean; |
| 10 | 10 | import org.springframework.context.annotation.Configuration; |
| 11 | -import org.springframework.context.annotation.Lazy; | |
| 12 | 11 | |
| 13 | 12 | import java.io.IOException; |
| 14 | 13 | |
| 15 | 14 | @Configuration(proxyBeanMethods = false) |
| 16 | -@Lazy(false) // 禁用懒加载,因为需要保证 Redis Server 必须先启动 | |
| 17 | 15 | @EnableConfigurationProperties(RedisProperties.class) |
| 18 | 16 | @AutoConfigureBefore({RedisAutoConfiguration.class, RedissonAutoConfiguration.class}) // 在 Redis 自动配置前,进行初始化 |
| 19 | 17 | public class RedisTestConfiguration { | ... | ... |
src/test/java/cn/iocoder/dashboard/config/SecurityTestConfiguration.java deleted
| 1 | -package cn.iocoder.dashboard.config; | |
| 2 | - | |
| 3 | -import org.mockito.Mockito; | |
| 4 | -import org.springframework.context.annotation.Bean; | |
| 5 | -import org.springframework.context.annotation.Configuration; | |
| 6 | -import org.springframework.security.authentication.AuthenticationManager; | |
| 7 | - | |
| 8 | -@Configuration | |
| 9 | -public class SecurityTestConfiguration { | |
| 10 | - | |
| 11 | - @Bean | |
| 12 | - public AuthenticationManager authenticationManager() { | |
| 13 | - return Mockito.mock(AuthenticationManager.class); | |
| 14 | - } | |
| 15 | - | |
| 16 | -} |
src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceTest.java
| 1 | 1 | package cn.iocoder.dashboard.modules.infra.service.config; |
| 2 | 2 | |
| 3 | -import cn.iocoder.dashboard.BaseSpringBootUnitTest; | |
| 3 | +import cn.iocoder.dashboard.BaseDbUnitTest; | |
| 4 | 4 | import cn.iocoder.dashboard.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigCreateReqVO; |
| 6 | 6 | import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigExportReqVO; |
| ... | ... | @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.util.collection.ArrayUtils; |
| 15 | 15 | import cn.iocoder.dashboard.util.object.ObjectUtils; |
| 16 | 16 | import org.junit.jupiter.api.Test; |
| 17 | 17 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 18 | +import org.springframework.context.annotation.Import; | |
| 18 | 19 | |
| 19 | 20 | import javax.annotation.Resource; |
| 20 | 21 | import java.util.List; |
| ... | ... | @@ -35,7 +36,8 @@ import static org.mockito.Mockito.verify; |
| 35 | 36 | * |
| 36 | 37 | * @author 芋道源码 |
| 37 | 38 | */ |
| 38 | -public class InfConfigServiceTest extends BaseSpringBootUnitTest { | |
| 39 | +@Import(InfConfigServiceImpl.class) | |
| 40 | +public class InfConfigServiceTest extends BaseDbUnitTest { | |
| 39 | 41 | |
| 40 | 42 | @Resource |
| 41 | 43 | private InfConfigServiceImpl configService; | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/system/service/auth/SysAuthServiceImplTest.java
| 1 | 1 | package cn.iocoder.dashboard.modules.system.service.auth; |
| 2 | 2 | |
| 3 | -import cn.iocoder.dashboard.BaseSpringBootUnitTest; | |
| 3 | +import cn.iocoder.dashboard.BaseDbUnitTest; | |
| 4 | 4 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.dashboard.framework.security.core.LoginUser; |
| 6 | 6 | import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; |
| 7 | 7 | import cn.iocoder.dashboard.modules.system.service.auth.impl.SysAuthServiceImpl; |
| 8 | +import cn.iocoder.dashboard.modules.system.service.common.SysCaptchaService; | |
| 9 | +import cn.iocoder.dashboard.modules.system.service.logger.SysLoginLogService; | |
| 8 | 10 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; |
| 9 | 11 | import cn.iocoder.dashboard.modules.system.service.user.SysUserService; |
| 10 | 12 | import cn.iocoder.dashboard.util.AssertUtils; |
| 11 | 13 | import org.junit.jupiter.api.Test; |
| 12 | 14 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 15 | +import org.springframework.context.annotation.Import; | |
| 16 | +import org.springframework.security.authentication.AuthenticationManager; | |
| 13 | 17 | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 14 | 18 | |
| 15 | 19 | import javax.annotation.Resource; |
| ... | ... | @@ -21,7 +25,13 @@ import static org.junit.jupiter.api.Assertions.*; |
| 21 | 25 | import static org.mockito.ArgumentMatchers.eq; |
| 22 | 26 | import static org.mockito.Mockito.when; |
| 23 | 27 | |
| 24 | -public class SysAuthServiceImplTest extends BaseSpringBootUnitTest { | |
| 28 | +/** | |
| 29 | + * {@link SysAuthServiceImpl} 的单元测试 | |
| 30 | + * | |
| 31 | + * @author 芋道源码 | |
| 32 | + */ | |
| 33 | +@Import(SysAuthServiceImpl.class) | |
| 34 | +public class SysAuthServiceImplTest extends BaseDbUnitTest { | |
| 25 | 35 | |
| 26 | 36 | @Resource |
| 27 | 37 | private SysAuthServiceImpl authService; |
| ... | ... | @@ -30,6 +40,14 @@ public class SysAuthServiceImplTest extends BaseSpringBootUnitTest { |
| 30 | 40 | private SysUserService userService; |
| 31 | 41 | @MockBean |
| 32 | 42 | private SysPermissionService permissionService; |
| 43 | + @MockBean | |
| 44 | + private AuthenticationManager authenticationManager; | |
| 45 | + @MockBean | |
| 46 | + private SysCaptchaService captchaService; | |
| 47 | + @MockBean | |
| 48 | + private SysLoginLogService loginLogService; | |
| 49 | + @MockBean | |
| 50 | + private SysUserSessionService userSessionService; | |
| 33 | 51 | |
| 34 | 52 | @Test |
| 35 | 53 | public void testLoadUserByUsername_success() { | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java
| 1 | 1 | package cn.iocoder.dashboard.modules.system.service.dict; |
| 2 | 2 | |
| 3 | -import cn.iocoder.dashboard.BaseSpringBootUnitTest; | |
| 3 | +import cn.iocoder.dashboard.BaseDbUnitTest; | |
| 4 | 4 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.dashboard.common.pojo.PageResult; |
| 6 | 6 | import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataCreateReqVO; |
| ... | ... | @@ -17,6 +17,7 @@ import cn.iocoder.dashboard.util.object.ObjectUtils; |
| 17 | 17 | import com.google.common.collect.ImmutableTable; |
| 18 | 18 | import org.junit.jupiter.api.Test; |
| 19 | 19 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 20 | +import org.springframework.context.annotation.Import; | |
| 20 | 21 | |
| 21 | 22 | import javax.annotation.Resource; |
| 22 | 23 | import java.util.Date; |
| ... | ... | @@ -37,7 +38,8 @@ import static org.mockito.Mockito.*; |
| 37 | 38 | * |
| 38 | 39 | * @author 芋道源码 |
| 39 | 40 | */ |
| 40 | -public class SysDictDataServiceTest extends BaseSpringBootUnitTest { | |
| 41 | +@Import(SysDictDataServiceImpl.class) | |
| 42 | +public class SysDictDataServiceTest extends BaseDbUnitTest { | |
| 41 | 43 | |
| 42 | 44 | @Resource |
| 43 | 45 | private SysDictDataServiceImpl dictDataService; | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java
| 1 | 1 | package cn.iocoder.dashboard.modules.system.service.dict; |
| 2 | 2 | |
| 3 | -import cn.iocoder.dashboard.BaseSpringBootUnitTest; | |
| 3 | +import cn.iocoder.dashboard.BaseDbUnitTest; | |
| 4 | 4 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| 5 | 5 | import cn.iocoder.dashboard.common.pojo.PageResult; |
| 6 | 6 | import cn.iocoder.dashboard.modules.system.controller.dict.vo.type.SysDictTypeCreateReqVO; |
| ... | ... | @@ -14,6 +14,7 @@ import cn.iocoder.dashboard.util.collection.ArrayUtils; |
| 14 | 14 | import cn.iocoder.dashboard.util.object.ObjectUtils; |
| 15 | 15 | import org.junit.jupiter.api.Test; |
| 16 | 16 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 17 | +import org.springframework.context.annotation.Import; | |
| 17 | 18 | |
| 18 | 19 | import javax.annotation.Resource; |
| 19 | 20 | import java.util.List; |
| ... | ... | @@ -24,7 +25,6 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; |
| 24 | 25 | import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; |
| 25 | 26 | import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; |
| 26 | 27 | import static cn.iocoder.dashboard.util.RandomUtils.*; |
| 27 | -import static cn.iocoder.dashboard.util.RandomUtils.randomString; | |
| 28 | 28 | import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; |
| 29 | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | 30 | import static org.mockito.ArgumentMatchers.eq; |
| ... | ... | @@ -35,7 +35,8 @@ import static org.mockito.Mockito.when; |
| 35 | 35 | * |
| 36 | 36 | * @author 芋道源码 |
| 37 | 37 | */ |
| 38 | -public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { | |
| 38 | +@Import(SysDictTypeServiceImpl.class) | |
| 39 | +public class SysDictTypeServiceTest extends BaseDbUnitTest { | |
| 39 | 40 | |
| 40 | 41 | @Resource |
| 41 | 42 | private SysDictTypeServiceImpl dictTypeService; | ... | ... |
src/test/resources/application-unit-test.yaml
| ... | ... | @@ -3,21 +3,6 @@ spring: |
| 3 | 3 | lazy-initialization: true # 开启懒加载,加快速度 |
| 4 | 4 | banner-mode: off # 单元测试,禁用 Banner |
| 5 | 5 | |
| 6 | - # 去除的自动配置项 | |
| 7 | - autoconfigure: | |
| 8 | - exclude: | |
| 9 | - - org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration # 单元测试,禁用 SpringSecurity | |
| 10 | - - org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration # 单元测试,禁用 SpringSecurity | |
| 11 | - - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration # 单元测试,禁用 Quartz | |
| 12 | - - com.baomidou.lock.spring.boot.autoconfigure.LockAutoConfiguration # 单元测试,禁用 Lock4j 分布式锁 | |
| 13 | - - org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration # 单元测试,禁用 Scheduler 定时任务 | |
| 14 | - - org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration # 项目没有使用 Spring Data,所以禁用配置类,加速启动 | |
| 15 | - | |
| 16 | -# Swagger 接口文档的自动配置(单元测试,禁用 Swagger) | |
| 17 | -springfox: | |
| 18 | - documentation: | |
| 19 | - auto-startup: false | |
| 20 | - | |
| 21 | 6 | --- #################### 数据库相关配置 #################### |
| 22 | 7 | |
| 23 | 8 | spring: |
| ... | ... | @@ -29,6 +14,9 @@ spring: |
| 29 | 14 | username: sa |
| 30 | 15 | password: |
| 31 | 16 | schema: classpath:sql/create_tables.sql # MySQL 转 H2 的语句,使用 https://www.jooq.org/translate/ 工具 |
| 17 | + druid: | |
| 18 | + async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度 | |
| 19 | + initial-size: 1 # 单元测试,配置为 1,提升启动速度 | |
| 32 | 20 | |
| 33 | 21 | # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 |
| 34 | 22 | redis: |
| ... | ... | @@ -36,17 +24,13 @@ spring: |
| 36 | 24 | port: 16379 # 端口(单元测试,使用 16379 端口) |
| 37 | 25 | database: 0 # 数据库索引 |
| 38 | 26 | |
| 39 | ---- #################### 定时任务相关配置 #################### | |
| 27 | +mybatis: | |
| 28 | + lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试 | |
| 40 | 29 | |
| 41 | -# Quartz 配置项,对应 QuartzProperties 配置类(单元测试,禁用 Quartz) | |
| 30 | +--- #################### 定时任务相关配置 #################### | |
| 42 | 31 | |
| 43 | 32 | --- #################### 配置中心相关配置 #################### |
| 44 | 33 | |
| 45 | -# Apollo 配置中心 | |
| 46 | -apollo: | |
| 47 | - bootstrap: | |
| 48 | - enabled: false # 单元测试,禁用配置中心 | |
| 49 | - | |
| 50 | 34 | --- #################### 服务保障相关配置 #################### |
| 51 | 35 | |
| 52 | 36 | # Lock4j 配置项(单元测试,禁用 Lock4j) |
| ... | ... | @@ -63,23 +47,6 @@ resilience4j: |
| 63 | 47 | |
| 64 | 48 | --- #################### 监控相关配置 #################### |
| 65 | 49 | |
| 66 | -# Actuator 监控端点的配置项 | |
| 67 | -management: | |
| 68 | - endpoints: | |
| 69 | - enabled-by-default: false | |
| 70 | - | |
| 71 | -# Spring Boot Admin 配置项 | |
| 72 | -spring: | |
| 73 | - boot: | |
| 74 | - admin: | |
| 75 | - # Spring Boot Admin Client 客户端的相关配置 | |
| 76 | - client: | |
| 77 | - enabled: false | |
| 78 | - # Spring Boot Admin Server 服务端的相关配置 | |
| 79 | - context-path: /admin # 配置 Spring | |
| 80 | - | |
| 81 | -# 日志文件配置(不需要配置) | |
| 82 | - | |
| 83 | 50 | --- #################### 芋道相关配置 #################### |
| 84 | 51 | |
| 85 | 52 | # 芋道配置项,设置当前项目所有自定义的配置 | ... | ... |
src/test/resources/sql/create_tables.sql
| ... | ... | @@ -9,9 +9,9 @@ CREATE TABLE IF NOT EXISTS "inf_config" ( |
| 9 | 9 | "value" varchar(500) NOT NULL DEFAULT '', |
| 10 | 10 | "sensitive" bit NOT NULL, |
| 11 | 11 | "remark" varchar(500) DEFAULT NULL, |
| 12 | - "create_by" varchar(64) DEFAULT '', | |
| 12 | + "creator" varchar(64) DEFAULT '', | |
| 13 | 13 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 14 | - "update_by" varchar(64) DEFAULT '', | |
| 14 | + "updater" varchar(64) DEFAULT '', | |
| 15 | 15 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 16 | 16 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 17 | 17 | PRIMARY KEY ("id") |
| ... | ... | @@ -28,9 +28,9 @@ CREATE TABLE IF NOT EXISTS "sys_dept" ( |
| 28 | 28 | "phone" varchar(11) DEFAULT NULL, |
| 29 | 29 | "email" varchar(50) DEFAULT NULL, |
| 30 | 30 | "status" tinyint NOT NULL, |
| 31 | - "create_by" varchar(64) DEFAULT '', | |
| 31 | + "creator" varchar(64) DEFAULT '', | |
| 32 | 32 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 33 | - "update_by" varchar(64) DEFAULT '', | |
| 33 | + "updater" varchar(64) DEFAULT '', | |
| 34 | 34 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 35 | 35 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 36 | 36 | PRIMARY KEY ("id") |
| ... | ... | @@ -44,9 +44,9 @@ CREATE TABLE IF NOT EXISTS "sys_dict_data" ( |
| 44 | 44 | "dict_type" varchar(100) NOT NULL DEFAULT '', |
| 45 | 45 | "status" tinyint NOT NULL DEFAULT '0', |
| 46 | 46 | "remark" varchar(500) DEFAULT NULL, |
| 47 | - "create_by" varchar(64) DEFAULT '', | |
| 47 | + "creator" varchar(64) DEFAULT '', | |
| 48 | 48 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 49 | - "update_by" varchar(64) DEFAULT '', | |
| 49 | + "updater" varchar(64) DEFAULT '', | |
| 50 | 50 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 51 | 51 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 52 | 52 | PRIMARY KEY ("id") |
| ... | ... | @@ -62,9 +62,9 @@ CREATE TABLE IF NOT EXISTS "sys_role" ( |
| 62 | 62 | "status" tinyint NOT NULL, |
| 63 | 63 | "type" tinyint NOT NULL, |
| 64 | 64 | "remark" varchar(500) DEFAULT NULL, |
| 65 | - "create_by" varchar(64) DEFAULT '', | |
| 65 | + "creator" varchar(64) DEFAULT '', | |
| 66 | 66 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 67 | - "update_by" varchar(64) DEFAULT '', | |
| 67 | + "updater" varchar(64) DEFAULT '', | |
| 68 | 68 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 69 | 69 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 70 | 70 | PRIMARY KEY ("id") |
| ... | ... | @@ -74,9 +74,9 @@ CREATE TABLE IF NOT EXISTS "sys_role_menu" ( |
| 74 | 74 | "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, |
| 75 | 75 | "role_id" bigint NOT NULL, |
| 76 | 76 | "menu_id" bigint NOT NULL, |
| 77 | - "create_by" varchar(64) DEFAULT '', | |
| 77 | + "creator" varchar(64) DEFAULT '', | |
| 78 | 78 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 79 | - "update_by" varchar(64) DEFAULT '', | |
| 79 | + "updater" varchar(64) DEFAULT '', | |
| 80 | 80 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 81 | 81 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 82 | 82 | PRIMARY KEY ("id") |
| ... | ... | @@ -93,9 +93,9 @@ CREATE TABLE IF NOT EXISTS "sys_menu" ( |
| 93 | 93 | "icon" varchar(100) DEFAULT '#', |
| 94 | 94 | "component" varchar(255) DEFAULT NULL, |
| 95 | 95 | "status" tinyint NOT NULL DEFAULT '0', |
| 96 | - "create_by" varchar(64) DEFAULT '', | |
| 96 | + "creator" varchar(64) DEFAULT '', | |
| 97 | 97 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 98 | - "update_by" varchar(64) DEFAULT '', | |
| 98 | + "updater" varchar(64) DEFAULT '', | |
| 99 | 99 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 100 | 100 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 101 | 101 | PRIMARY KEY ("id") |
| ... | ... | @@ -107,9 +107,9 @@ CREATE TABLE "sys_dict_type" ( |
| 107 | 107 | "type" varchar(100) NOT NULL DEFAULT '', |
| 108 | 108 | "status" tinyint NOT NULL DEFAULT '0', |
| 109 | 109 | "remark" varchar(500) DEFAULT NULL, |
| 110 | - "create_by" varchar(64) DEFAULT '', | |
| 110 | + "creator" varchar(64) DEFAULT '', | |
| 111 | 111 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 112 | - "update_by" varchar(64) DEFAULT '', | |
| 112 | + "updater" varchar(64) DEFAULT '', | |
| 113 | 113 | "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 114 | 114 | "deleted" bit NOT NULL DEFAULT FALSE, |
| 115 | 115 | PRIMARY KEY ("id") | ... | ... |