mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 21:32:07 +08:00
手工调账service、controller单元测试完善
This commit is contained in:
+87
@@ -0,0 +1,87 @@
|
||||
package com.cf.imes.module.system.controller.admin.funds.manualadjust;
|
||||
|
||||
import com.cf.imes.framework.common.pojo.PageResult;
|
||||
import com.cf.imes.module.system.controller.admin.funds.manualadjust.vo.ManualAdjustAccountBalancePageReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.funds.manualadjust.vo.ManualAdjustAccountBalanceRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.funds.manualadjust.vo.ManualAdjustAccountBalanceSaveReqVO;
|
||||
import com.cf.imes.module.system.enums.pay.AdjustTypeEnum;
|
||||
import com.cf.imes.module.system.service.funds.manualadjust.ManualAdjustAccountBalanceService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* @author Gqr
|
||||
* @since 2025/10/27 14:31
|
||||
*/
|
||||
@WebMvcTest(controllers = ManualAdjustAccountBalanceController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
|
||||
public class ManualAdjustAccountBalanceControllerTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockitoBean
|
||||
private ManualAdjustAccountBalanceService manualAdjustAccountBalanceService;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
void testCreateAjustment() throws Exception {
|
||||
ManualAdjustAccountBalanceSaveReqVO reqVO = new ManualAdjustAccountBalanceSaveReqVO();
|
||||
reqVO.setOrganId(1L);
|
||||
reqVO.setAdjustType(1);
|
||||
reqVO.setRechargeAmount(new BigDecimal("100"));
|
||||
reqVO.setGiftAmount(new BigDecimal("20"));
|
||||
|
||||
when(manualAdjustAccountBalanceService.createAjustment(any())).thenReturn(123L);
|
||||
|
||||
mockMvc.perform(put("/system/balance/manualadjust")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(reqVO)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(123));
|
||||
|
||||
verify(manualAdjustAccountBalanceService).createAjustment(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAdjustPage() throws Exception {
|
||||
ManualAdjustAccountBalancePageReqVO pageReqVO = new ManualAdjustAccountBalancePageReqVO();
|
||||
pageReqVO.setPageNo(1);
|
||||
pageReqVO.setPageSize(10);
|
||||
|
||||
ManualAdjustAccountBalanceRespVO respVO = new ManualAdjustAccountBalanceRespVO();
|
||||
respVO.setOrganName("测试组织");
|
||||
respVO.setAdjustType(AdjustTypeEnum.INCREASE.getType());
|
||||
|
||||
PageResult<ManualAdjustAccountBalanceRespVO> pageResult = new PageResult<>(
|
||||
Collections.singletonList(respVO), 1L);
|
||||
|
||||
when(manualAdjustAccountBalanceService.getPage(any())).thenReturn(pageResult);
|
||||
|
||||
mockMvc.perform(get("/system/balance/manualadjust/page")
|
||||
.param("pageNo", "1")
|
||||
.param("pageSize", "10"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.list[0].organName").value("测试组织"))
|
||||
.andExpect(jsonPath("$.data.total").value(1));
|
||||
|
||||
verify(manualAdjustAccountBalanceService).getPage(any());
|
||||
}
|
||||
}
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
package com.cf.imes.module.system.service.funds.manualadjust;
|
||||
|
||||
import com.cf.imes.framework.common.pojo.PageResult;
|
||||
import com.cf.imes.framework.id.core.util.SnowflakeSerialNoWorker3rd;
|
||||
import com.cf.imes.framework.test.core.ut.BaseDbAndRedisUnitTest;
|
||||
import com.cf.imes.module.system.controller.admin.funds.manualadjust.vo.ManualAdjustAccountBalancePageReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.funds.manualadjust.vo.ManualAdjustAccountBalanceRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.funds.manualadjust.vo.ManualAdjustAccountBalanceSaveReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.funds.organamount.vo.OrganRechargeAmountRespVO;
|
||||
import com.cf.imes.module.system.dal.dataobject.funds.organamount.OrganAmountDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.incomeandexpense.IncomeExpenseDetailsDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.pay.PayOrderDO;
|
||||
import com.cf.imes.module.system.enums.pay.AdjustTypeEnum;
|
||||
import com.cf.imes.module.system.framework.snowflake.config.MybatisIdProperties;
|
||||
import com.cf.imes.module.system.service.funds.incomeandexpense.IncomeExpenseService;
|
||||
import com.cf.imes.module.system.service.funds.organamount.OrganAmountService;
|
||||
import com.cf.imes.module.system.service.organ.OrganService;
|
||||
import com.cf.imes.module.system.service.pay.PayOrderService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
|
||||
import static com.cf.imes.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static com.cf.imes.module.system.enums.ErrorCodeConstants.ORGAMOUNT_GIFT_INSUFFICIENT_ERROR;
|
||||
import static com.cf.imes.module.system.enums.ErrorCodeConstants.ORGAMOUNT_RECHARGE_INSUFFICIENT_ERROR;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Gqr
|
||||
* @since 2025/10/27 11:57
|
||||
*/
|
||||
@Import(ManualAdjustAccountBalanceServiceImpl.class)
|
||||
public class ManualAdjustAccountBalanceServiceImplTest extends BaseDbAndRedisUnitTest {
|
||||
@Resource
|
||||
private ManualAdjustAccountBalanceService manualAdjustAccountBalanceService;
|
||||
|
||||
@MockitoBean
|
||||
private OrganService organService;
|
||||
|
||||
@MockitoBean
|
||||
private OrganAmountService organAmountService;
|
||||
|
||||
@MockitoBean
|
||||
private IncomeExpenseService incomeExpenseService;
|
||||
|
||||
@MockitoBean
|
||||
private MybatisIdProperties mybatisIdProperties;
|
||||
|
||||
@MockitoBean
|
||||
private PayOrderService payOrderService;
|
||||
|
||||
@MockitoBean
|
||||
private SnowflakeSerialNoWorker3rd snowflakeSerialNoWorker3rd;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
when(mybatisIdProperties.getWorkerId()).thenReturn(1L);
|
||||
when(mybatisIdProperties.getDatacenterId()).thenReturn(1L);
|
||||
when(snowflakeSerialNoWorker3rd.nextSerialNo()).thenReturn("SN-001");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAdjustment_Increase() {
|
||||
// 模拟请求参数(调增)
|
||||
ManualAdjustAccountBalanceSaveReqVO reqVO = new ManualAdjustAccountBalanceSaveReqVO();
|
||||
reqVO.setOrganId(100L);
|
||||
reqVO.setAdjustType(AdjustTypeEnum.INCREASE.getType());
|
||||
reqVO.setRechargeAmount(new BigDecimal("100.00"));
|
||||
reqVO.setGiftAmount(new BigDecimal("20.00"));
|
||||
reqVO.setRemark("调增测试");
|
||||
|
||||
// 模拟依赖返回
|
||||
OrganAmountDO organAmountDO = new OrganAmountDO();
|
||||
organAmountDO.setAmount(new BigDecimal("500.00"));
|
||||
organAmountDO.setGiftAmount(new BigDecimal("100.00"));
|
||||
when(organAmountService.validOrganAmount(100L)).thenReturn(organAmountDO);
|
||||
|
||||
OrganRechargeAmountRespVO rechargeRespVO = new OrganRechargeAmountRespVO();
|
||||
rechargeRespVO.setAmount(new BigDecimal("600.00"));
|
||||
rechargeRespVO.setGiftAmount(new BigDecimal("120.00"));
|
||||
rechargeRespVO.setAvailableAmount(new BigDecimal("720.00"));
|
||||
when(organAmountService.recharge(eq(100L), any(), any())).thenReturn(rechargeRespVO);
|
||||
|
||||
when(incomeExpenseService.createDetail(any())).thenReturn(1L);
|
||||
|
||||
// 执行方法
|
||||
Long result = manualAdjustAccountBalanceService.createAjustment(reqVO);
|
||||
|
||||
// 验证逻辑
|
||||
assertEquals(1L, result);
|
||||
verify(organService).validOrgan(100L);
|
||||
verify(payOrderService).createPayOrder(any(PayOrderDO.class));
|
||||
verify(incomeExpenseService).createDetail(any(IncomeExpenseDetailsDO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAdjustment_Decrease() {
|
||||
// 模拟请求参数(调减)
|
||||
ManualAdjustAccountBalanceSaveReqVO reqVO = new ManualAdjustAccountBalanceSaveReqVO();
|
||||
reqVO.setOrganId(200L);
|
||||
reqVO.setAdjustType(AdjustTypeEnum.DECREASE.getType());
|
||||
reqVO.setRechargeAmount(new BigDecimal("50.00"));
|
||||
reqVO.setGiftAmount(new BigDecimal("10.00"));
|
||||
reqVO.setRemark("调减测试");
|
||||
|
||||
// 模拟依赖返回
|
||||
OrganAmountDO organAmountDO = new OrganAmountDO();
|
||||
organAmountDO.setAmount(new BigDecimal("500.00"));
|
||||
organAmountDO.setGiftAmount(new BigDecimal("100.00"));
|
||||
when(organAmountService.validOrganAmount(200L)).thenReturn(organAmountDO);
|
||||
|
||||
OrganRechargeAmountRespVO rechargeRespVO = new OrganRechargeAmountRespVO();
|
||||
rechargeRespVO.setAmount(new BigDecimal("450.00"));
|
||||
rechargeRespVO.setGiftAmount(new BigDecimal("90.00"));
|
||||
rechargeRespVO.setAvailableAmount(new BigDecimal("540.00"));
|
||||
when(organAmountService.recharge(eq(200L), any(), any())).thenReturn(rechargeRespVO);
|
||||
|
||||
when(incomeExpenseService.createDetail(any())).thenReturn(2L);
|
||||
|
||||
// 执行方法
|
||||
Long result = manualAdjustAccountBalanceService.createAjustment(reqVO);
|
||||
|
||||
// 验证逻辑
|
||||
assertEquals(2L, result);
|
||||
verify(organService).validOrgan(200L);
|
||||
verify(payOrderService).createPayOrder(any(PayOrderDO.class));
|
||||
verify(incomeExpenseService).createDetail(any(IncomeExpenseDetailsDO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAdjustment_Decrease_RechargeInsufficient() {
|
||||
// given
|
||||
ManualAdjustAccountBalanceSaveReqVO reqVO = new ManualAdjustAccountBalanceSaveReqVO();
|
||||
reqVO.setOrganId(300L);
|
||||
reqVO.setAdjustType(AdjustTypeEnum.DECREASE.getType());
|
||||
reqVO.setRechargeAmount(new BigDecimal("200.00")); // 想减200
|
||||
reqVO.setGiftAmount(null);
|
||||
|
||||
OrganAmountDO organAmountDO = new OrganAmountDO();
|
||||
organAmountDO.setAmount(new BigDecimal("100.00")); // 实际余额只有100,不够减
|
||||
organAmountDO.setGiftAmount(new BigDecimal("50.00"));
|
||||
when(organAmountService.validOrganAmount(300L)).thenReturn(organAmountDO);
|
||||
|
||||
// 余额不足
|
||||
assertServiceException(() -> manualAdjustAccountBalanceService.createAjustment(reqVO), ORGAMOUNT_RECHARGE_INSUFFICIENT_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAdjustment_Decrease_GiftInsufficient() {
|
||||
// given
|
||||
ManualAdjustAccountBalanceSaveReqVO reqVO = new ManualAdjustAccountBalanceSaveReqVO();
|
||||
reqVO.setOrganId(400L);
|
||||
reqVO.setAdjustType(AdjustTypeEnum.DECREASE.getType());
|
||||
reqVO.setRechargeAmount(null);
|
||||
reqVO.setGiftAmount(new BigDecimal("30.00")); // 想减30赠送金
|
||||
|
||||
OrganAmountDO organAmountDO = new OrganAmountDO();
|
||||
organAmountDO.setAmount(new BigDecimal("100.00"));
|
||||
organAmountDO.setGiftAmount(new BigDecimal("10.00")); // 实际赠送金只有10,不够减
|
||||
when(organAmountService.validOrganAmount(400L)).thenReturn(organAmountDO);
|
||||
|
||||
// 赠送金不足
|
||||
assertServiceException(() -> manualAdjustAccountBalanceService.createAjustment(reqVO), ORGAMOUNT_GIFT_INSUFFICIENT_ERROR);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAdjustment_Decrease_GiftAmountNull() {
|
||||
// given
|
||||
ManualAdjustAccountBalanceSaveReqVO reqVO = new ManualAdjustAccountBalanceSaveReqVO();
|
||||
reqVO.setOrganId(600L);
|
||||
reqVO.setAdjustType(AdjustTypeEnum.DECREASE.getType());
|
||||
reqVO.setRechargeAmount(new BigDecimal("50.00")); // 有充值金额
|
||||
reqVO.setGiftAmount(null); // giftAmount 为 null
|
||||
|
||||
OrganAmountDO organAmountDO = new OrganAmountDO();
|
||||
organAmountDO.setAmount(new BigDecimal("100.00"));
|
||||
organAmountDO.setGiftAmount(new BigDecimal("100.00"));
|
||||
when(organAmountService.validOrganAmount(600L)).thenReturn(organAmountDO);
|
||||
|
||||
OrganRechargeAmountRespVO respVO = new OrganRechargeAmountRespVO();
|
||||
respVO.setAmount(new BigDecimal("50.00"));
|
||||
respVO.setGiftAmount(new BigDecimal("100.00"));
|
||||
respVO.setAvailableAmount(new BigDecimal("150.00"));
|
||||
when(organAmountService.recharge(eq(600L), any(), any())).thenReturn(respVO);
|
||||
when(incomeExpenseService.createDetail(any())).thenReturn(10L);
|
||||
|
||||
// when
|
||||
Long result = manualAdjustAccountBalanceService.createAjustment(reqVO);
|
||||
|
||||
// then
|
||||
assertEquals(10L, result);
|
||||
verify(payOrderService).createPayOrder(any(PayOrderDO.class));
|
||||
verify(incomeExpenseService).createDetail(any(IncomeExpenseDetailsDO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAdjustment_Decrease_RechargeAmountNull() {
|
||||
// given
|
||||
ManualAdjustAccountBalanceSaveReqVO reqVO = new ManualAdjustAccountBalanceSaveReqVO();
|
||||
reqVO.setOrganId(600L);
|
||||
reqVO.setAdjustType(AdjustTypeEnum.DECREASE.getType());
|
||||
reqVO.setRechargeAmount(null); // rechargeAmount 为 null
|
||||
reqVO.setGiftAmount(new BigDecimal("50.00"));
|
||||
|
||||
OrganAmountDO organAmountDO = new OrganAmountDO();
|
||||
organAmountDO.setAmount(new BigDecimal("100.00"));
|
||||
organAmountDO.setGiftAmount(new BigDecimal("100.00"));
|
||||
when(organAmountService.validOrganAmount(600L)).thenReturn(organAmountDO);
|
||||
|
||||
OrganRechargeAmountRespVO respVO = new OrganRechargeAmountRespVO();
|
||||
respVO.setAmount(new BigDecimal("50.00"));
|
||||
respVO.setGiftAmount(new BigDecimal("100.00"));
|
||||
respVO.setAvailableAmount(new BigDecimal("150.00"));
|
||||
when(organAmountService.recharge(eq(600L), any(), any())).thenReturn(respVO);
|
||||
when(incomeExpenseService.createDetail(any())).thenReturn(10L);
|
||||
|
||||
// when
|
||||
Long result = manualAdjustAccountBalanceService.createAjustment(reqVO);
|
||||
|
||||
// then
|
||||
assertEquals(10L, result);
|
||||
verify(payOrderService, never()).createPayOrder(any(PayOrderDO.class));
|
||||
verify(incomeExpenseService).createDetail(any(IncomeExpenseDetailsDO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPage_WithNonNullOrgan() {
|
||||
// given
|
||||
ManualAdjustAccountBalancePageReqVO pageReqVO = new ManualAdjustAccountBalancePageReqVO();
|
||||
|
||||
IncomeExpenseDetailsDO detailsDO = new IncomeExpenseDetailsDO();
|
||||
detailsDO.setIncomeExpenseType(AdjustTypeEnum.INCREASE.getType()); // 假设 1 -> 调增
|
||||
detailsDO.setOrganId(100L);
|
||||
|
||||
PageResult<IncomeExpenseDetailsDO> mockPageResult = new PageResult<>(Collections.singletonList(detailsDO), 1L);
|
||||
when(incomeExpenseService.selectAdjustPage(pageReqVO)).thenReturn(mockPageResult);
|
||||
|
||||
OrganizationDO organ = new OrganizationDO();
|
||||
organ.setName("测试组织");
|
||||
when(organService.getOrganWithDeleted(100L)).thenReturn(organ);
|
||||
|
||||
// when
|
||||
PageResult<ManualAdjustAccountBalanceRespVO> result = manualAdjustAccountBalanceService.getPage(pageReqVO);
|
||||
|
||||
// then
|
||||
assertEquals(1, result.getList().size());
|
||||
ManualAdjustAccountBalanceRespVO respVO = result.getList().get(0);
|
||||
assertEquals("测试组织", respVO.getOrganName());
|
||||
assertEquals(AdjustTypeEnum.INCREASE.getType(), respVO.getAdjustType());
|
||||
|
||||
verify(incomeExpenseService).selectAdjustPage(pageReqVO);
|
||||
verify(organService).getOrganWithDeleted(100L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetPage_WithNullOrgan() {
|
||||
// given
|
||||
ManualAdjustAccountBalancePageReqVO pageReqVO = new ManualAdjustAccountBalancePageReqVO();
|
||||
|
||||
IncomeExpenseDetailsDO detailsDO = new IncomeExpenseDetailsDO();
|
||||
detailsDO.setIncomeExpenseType(AdjustTypeEnum.DECREASE.getType()); // 假设 2 -> 调减
|
||||
detailsDO.setOrganId(200L);
|
||||
|
||||
PageResult<IncomeExpenseDetailsDO> mockPageResult = new PageResult<>(Collections.singletonList(detailsDO), 1L);
|
||||
when(incomeExpenseService.selectAdjustPage(pageReqVO)).thenReturn(mockPageResult);
|
||||
|
||||
// 模拟组织为 null,覆盖 if 条件 false
|
||||
when(organService.getOrganWithDeleted(200L)).thenReturn(null);
|
||||
|
||||
// when
|
||||
PageResult<ManualAdjustAccountBalanceRespVO> result = manualAdjustAccountBalanceService.getPage(pageReqVO);
|
||||
|
||||
// then
|
||||
assertEquals(1, result.getList().size());
|
||||
ManualAdjustAccountBalanceRespVO respVO = result.getList().get(0);
|
||||
assertNull(respVO.getOrganName());
|
||||
assertEquals(AdjustTypeEnum.DECREASE.getType(), respVO.getAdjustType());
|
||||
|
||||
verify(incomeExpenseService).selectAdjustPage(pageReqVO);
|
||||
verify(organService).getOrganWithDeleted(200L);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user