From 06634a426584d663a58c57b35c9aaffbd9757810 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 25 Jan 2025 10:09:39 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8D=95=E6=B5=8B=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91system=EF=BC=9AAdminAuthServiceImplTest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/auth/AdminAuthServiceImpl.java | 2 ++ .../auth/AdminAuthServiceImplTest.java | 27 +++---------------- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java index e81c75724c..4b09980235 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java +++ b/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; import com.xingyuv.captcha.service.CaptchaService; import jakarta.annotation.Resource; import jakarta.validation.Validator; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -71,6 +72,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { * 验证码的开关,默认为 true */ @Value("${yudao.captcha.enable:true}") + @Setter // 为了单测:开启或者关闭验证码 private Boolean captchaEnable; @Override diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java index 2f5f717e24..151150bc5a 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java +++ b/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; import com.xingyuv.captcha.model.common.ResponseModel; import com.xingyuv.captcha.service.CaptchaService; import jakarta.annotation.Resource; -import jakarta.validation.ConstraintViolationException; import jakarta.validation.Validation; import jakarta.validation.Validator; import org.junit.jupiter.api.BeforeEach; @@ -37,7 +36,6 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; @@ -66,7 +64,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { @BeforeEach public void setUp() { - ReflectUtil.setFieldValue(authService, "captchaEnable", true); + authService.setCaptchaEnable(true); // 注入一个 Validator 对象 ReflectUtil.setFieldValue(authService, "validator", Validation.buildDefaultValidatorFactory().getValidator()); @@ -156,7 +154,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { .setSocialType(randomEle(SocialTypeEnum.values()).getType())); // mock 验证码正确 - ReflectUtil.setFieldValue(authService, "captchaEnable", false); + authService.setCaptchaEnable(false); // mock user 数据 AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username") .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus())); @@ -269,8 +267,6 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { // 准备参数 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); - // mock 验证码打开 - ReflectUtil.setFieldValue(authService, "captchaEnable", true); // mock 验证通过 when(captchaService.verification(argThat(captchaVO -> { assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification()); @@ -287,34 +283,17 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest { AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); // mock 验证码关闭 - ReflectUtil.setFieldValue(authService, "captchaEnable", false); + authService.setCaptchaEnable(false); // 调用,无需断言 authService.validateCaptcha(reqVO); } - @Test - public void testValidateCaptcha_constraintViolationException() { - // 准备参数 - AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); - reqVO.setCaptchaVerification(null); - - // mock 验证码打开 - ReflectUtil.setFieldValue(authService, "captchaEnable", true); - - // 调用,并断言异常 - assertThrows(ConstraintViolationException.class, () -> authService.validateCaptcha(reqVO), - "验证码不能为空"); - } - - @Test public void testCaptcha_fail() { // 准备参数 AuthLoginReqVO reqVO = randomPojo(AuthLoginReqVO.class); - // mock 验证码打开 - ReflectUtil.setFieldValue(authService, "captchaEnable", true); // mock 验证通过 when(captchaService.verification(argThat(captchaVO -> { assertEquals(reqVO.getCaptchaVerification(), captchaVO.getCaptchaVerification());