Commit 06634a426584d663a58c57b35c9aaffbd9757810

Authored by YunaiV
1 parent 534c6470

【单测修复】system:AdminAuthServiceImplTest

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
@@ -30,6 +30,7 @@ import com.xingyuv.captcha.model.vo.CaptchaVO; @@ -30,6 +30,7 @@ import com.xingyuv.captcha.model.vo.CaptchaVO;
30 import com.xingyuv.captcha.service.CaptchaService; 30 import com.xingyuv.captcha.service.CaptchaService;
31 import jakarta.annotation.Resource; 31 import jakarta.annotation.Resource;
32 import jakarta.validation.Validator; 32 import jakarta.validation.Validator;
  33 +import lombok.Setter;
33 import lombok.extern.slf4j.Slf4j; 34 import lombok.extern.slf4j.Slf4j;
34 import org.springframework.beans.factory.annotation.Value; 35 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.stereotype.Service; 36 import org.springframework.stereotype.Service;
@@ -71,6 +72,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { @@ -71,6 +72,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
71 * 验证码的开关,默认为 true 72 * 验证码的开关,默认为 true
72 */ 73 */
73 @Value("${yudao.captcha.enable:true}") 74 @Value("${yudao.captcha.enable:true}")
  75 + @Setter // 为了单测:开启或者关闭验证码
74 private Boolean captchaEnable; 76 private Boolean captchaEnable;
75 77
76 @Override 78 @Override
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java
@@ -22,7 +22,6 @@ import cn.iocoder.yudao.module.system.service.user.AdminUserService; @@ -22,7 +22,6 @@ import cn.iocoder.yudao.module.system.service.user.AdminUserService;
22 import com.xingyuv.captcha.model.common.ResponseModel; 22 import com.xingyuv.captcha.model.common.ResponseModel;
23 import com.xingyuv.captcha.service.CaptchaService; 23 import com.xingyuv.captcha.service.CaptchaService;
24 import jakarta.annotation.Resource; 24 import jakarta.annotation.Resource;
25 -import jakarta.validation.ConstraintViolationException;  
26 import jakarta.validation.Validation; 25 import jakarta.validation.Validation;
27 import jakarta.validation.Validator; 26 import jakarta.validation.Validator;
28 import org.junit.jupiter.api.BeforeEach; 27 import org.junit.jupiter.api.BeforeEach;
@@ -37,7 +36,6 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; @@ -37,7 +36,6 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
37 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; 36 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
38 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; 37 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
39 import static org.junit.jupiter.api.Assertions.assertEquals; 38 import static org.junit.jupiter.api.Assertions.assertEquals;
40 -import static org.junit.jupiter.api.Assertions.assertThrows;  
41 import static org.mockito.ArgumentMatchers.eq; 39 import static org.mockito.ArgumentMatchers.eq;
42 import static org.mockito.Mockito.*; 40 import static org.mockito.Mockito.*;
43 41
@@ -66,7 +64,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { @@ -66,7 +64,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
66 64
67 @BeforeEach 65 @BeforeEach
68 public void setUp() { 66 public void setUp() {
69 - ReflectUtil.setFieldValue(authService, "captchaEnable", true); 67 + authService.setCaptchaEnable(true);
70 // 注入一个 Validator 对象 68 // 注入一个 Validator 对象
71 ReflectUtil.setFieldValue(authService, "validator", 69 ReflectUtil.setFieldValue(authService, "validator",
72 Validation.buildDefaultValidatorFactory().getValidator()); 70 Validation.buildDefaultValidatorFactory().getValidator());
@@ -156,7 +154,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { @@ -156,7 +154,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
156 .setSocialType(randomEle(SocialTypeEnum.values()).getType())); 154 .setSocialType(randomEle(SocialTypeEnum.values()).getType()));
157 155
158 // mock 验证码正确 156 // mock 验证码正确
159 - ReflectUtil.setFieldValue(authService, "captchaEnable", false); 157 + authService.setCaptchaEnable(false);
160 // mock user 数据 158 // mock user 数据
161 AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username") 159 AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username")
162 .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus())); 160 .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus()));
@@ -269,8 +267,6 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { @@ -269,8 +267,6 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
269 // 准备参数 267 // 准备参数
270 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); 268 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
271 269
272 - // mock 验证码打开  
273 - ReflectUtil.setFieldValue(authService, "captchaEnable", true);  
274 // mock 验证通过 270 // mock 验证通过
275 when(captchaService.verification(argThat(captchaVO -> { 271 when(captchaService.verification(argThat(captchaVO -> {
276 assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification()); 272 assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());
@@ -287,34 +283,17 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { @@ -287,34 +283,17 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
287 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); 283 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
288 284
289 // mock 验证码关闭 285 // mock 验证码关闭
290 - ReflectUtil.setFieldValue(authService, "captchaEnable", false); 286 + authService.setCaptchaEnable(false);
291 287
292 // 调用,无需断言 288 // 调用,无需断言
293 authService.validateCaptcha(reqVO); 289 authService.validateCaptcha(reqVO);
294 } 290 }
295 291
296 @Test 292 @Test
297 - public void testValidateCaptcha_constraintViolationException() {  
298 - // 准备参数  
299 - AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);  
300 - reqVO.setCaptchaVerification(null);  
301 -  
302 - // mock 验证码打开  
303 - ReflectUtil.setFieldValue(authService, "captchaEnable", true);  
304 -  
305 - // 调用,并断言异常  
306 - assertThrows(ConstraintViolationException.class, () -> authService.validateCaptcha(reqVO),  
307 - "验证码不能为空");  
308 - }  
309 -  
310 -  
311 - @Test  
312 public void testCaptcha_fail() { 293 public void testCaptcha_fail() {
313 // 准备参数 294 // 准备参数
314 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); 295 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class);
315 296
316 - // mock 验证码打开  
317 - ReflectUtil.setFieldValue(authService, "captchaEnable", true);  
318 // mock 验证通过 297 // mock 验证通过
319 when(captchaService.verification(argThat(captchaVO -> { 298 when(captchaService.verification(argThat(captchaVO -> {
320 assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification()); 299 assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());