mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 13:22:07 +08:00
1、充值活动controller单元测试完善;2、充值明细无用方法移除、service/controller单元测试完善;
This commit is contained in:
+164
@@ -0,0 +1,164 @@
|
||||
package com.cf.imes.module.system.controller.admin.funds.recharge;
|
||||
|
||||
import com.cf.imes.framework.common.pojo.PageResult;
|
||||
import com.cf.imes.module.system.controller.admin.funds.recharge.vo.RechargePageReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.funds.recharge.vo.RechargeSaveReqVO;
|
||||
import com.cf.imes.module.system.dal.dataobject.funds.recharge.GiftMoneyDetailsDO;
|
||||
import com.cf.imes.module.system.enums.recharge.RechargeGiftTypeEnum;
|
||||
import com.cf.imes.module.system.enums.recharge.RechargeTypeEnum;
|
||||
import com.cf.imes.module.system.service.funds.recharge.RechargeActiveService;
|
||||
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.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
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 9:29
|
||||
*/
|
||||
@WebMvcTest(controllers = RechargeActiveController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
|
||||
class RechargeActiveControllerTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockitoBean
|
||||
private RechargeActiveService rechargeActiveService;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
void testCreateRechargeActive() throws Exception {
|
||||
RechargeSaveReqVO vo = new RechargeSaveReqVO();
|
||||
vo.setActivityName("充值活动");
|
||||
vo.setRechargeGiftType(RechargeGiftTypeEnum.EVERY_X_GIVE_X.getStatus());
|
||||
vo.setRechargeType(RechargeTypeEnum.ALIPAY.getStatus());
|
||||
vo.setRechargeAmount(BigDecimal.valueOf(100));
|
||||
vo.setGiftAmount(BigDecimal.valueOf(100));
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
vo.setStartTime(now);
|
||||
vo.setEndTime(now);
|
||||
|
||||
// mock Service 返回
|
||||
when(rechargeActiveService.create(any())).thenReturn(100L);
|
||||
|
||||
mockMvc.perform(put("/system/recharge-active")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(vo)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(100));
|
||||
|
||||
// 验证 Service 调用
|
||||
verify(rechargeActiveService, times(1)).create(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRechargeActive() throws Exception {
|
||||
RechargeSaveReqVO vo = new RechargeSaveReqVO();
|
||||
vo.setId(1L);
|
||||
vo.setActivityName("更新充值活动");
|
||||
vo.setRechargeGiftType(RechargeGiftTypeEnum.EVERY_X_GIVE_X.getStatus());
|
||||
vo.setRechargeType(RechargeTypeEnum.ALIPAY.getStatus());
|
||||
vo.setRechargeAmount(BigDecimal.valueOf(100));
|
||||
vo.setGiftAmount(BigDecimal.valueOf(100));
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
vo.setStartTime(now);
|
||||
vo.setEndTime(now);
|
||||
|
||||
// mock Service 返回
|
||||
doNothing().when(rechargeActiveService).update(any());
|
||||
|
||||
mockMvc.perform(post("/system/recharge-active")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(vo)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
// 验证 Service 调用
|
||||
verify(rechargeActiveService, times(1)).update(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteRechargeActive() throws Exception {
|
||||
Long id = 3L;
|
||||
doNothing().when(rechargeActiveService).delete(anyLong());
|
||||
|
||||
mockMvc.perform(delete("/system/recharge-active/{id}", id))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
|
||||
verify(rechargeActiveService, times(1)).delete(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRechargeActive() throws Exception {
|
||||
Long id = 123L;
|
||||
GiftMoneyDetailsDO mockDO = new GiftMoneyDetailsDO()
|
||||
.setId(id);
|
||||
when(rechargeActiveService.get(id)).thenReturn(mockDO);
|
||||
|
||||
mockMvc.perform(get("/system/recharge-active/{id}", id))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.id").value(123L));
|
||||
|
||||
verify(rechargeActiveService, times(1)).get(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRechargeActivePage() throws Exception {
|
||||
RechargePageReqVO reqVO = new RechargePageReqVO();
|
||||
PageResult<GiftMoneyDetailsDO> mockPage = new PageResult<>();
|
||||
mockPage.setTotal(1L);
|
||||
mockPage.setList(List.of(new GiftMoneyDetailsDO().setId(1L)));
|
||||
when(rechargeActiveService.getPage(any())).thenReturn(mockPage);
|
||||
|
||||
mockMvc.perform(get("/system/recharge-active/page")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(reqVO)))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.list[0].id").value(1L));
|
||||
|
||||
verify(rechargeActiveService, times(1)).getPage(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailableRechargeActivePage_success() throws Exception {
|
||||
// 模拟 service 返回数据
|
||||
GiftMoneyDetailsDO mockDO = new GiftMoneyDetailsDO();
|
||||
mockDO.setId(1L);
|
||||
mockDO.setRechargeAmount(new BigDecimal("50.00"));
|
||||
mockDO.setActivityName("新人充值活动");
|
||||
|
||||
when(rechargeActiveService.getAvailableRechargeActiveList())
|
||||
.thenReturn(List.of(mockDO));
|
||||
|
||||
// 发起请求
|
||||
mockMvc.perform(get("/system/recharge-active/available")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(0)) // CommonResult.success 通常 code=0
|
||||
.andExpect(jsonPath("$.data[0].activityName").value("新人充值活动"))
|
||||
.andExpect(jsonPath("$.data[0].rechargeAmount").value(50.00));
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.cf.imes.module.system.controller.admin.funds.recharge;
|
||||
|
||||
import com.cf.imes.module.system.controller.admin.funds.recharge.vo.RechargeDetailsBalanceInfoRespVO;
|
||||
import com.cf.imes.module.system.service.funds.recharge.RechargeDetailsService;
|
||||
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 static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
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 10:56
|
||||
*/
|
||||
@WebMvcTest(controllers = RechargeDetailsController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
|
||||
class RechargeDetailsControllerTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockitoBean
|
||||
private RechargeDetailsService rechargeDetailsService;
|
||||
|
||||
@Test
|
||||
void testGetManageRechargeDetailsBalanceInfo_success() throws Exception {
|
||||
// 构造 mock 返回对象
|
||||
RechargeDetailsBalanceInfoRespVO respVO = new RechargeDetailsBalanceInfoRespVO();
|
||||
respVO.setRechargeAmount(new BigDecimal("100.00"));
|
||||
respVO.setRechargeUsageAmount(new BigDecimal("60.00"));
|
||||
|
||||
when(rechargeDetailsService.getManageRechargeDetailsBalanceInfo()).thenReturn(respVO);
|
||||
|
||||
// 执行 GET /recharge/balance
|
||||
mockMvc.perform(get("/system/rechargeDetails/balance")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(0)) // CommonResult.success 通常 code = 0
|
||||
.andExpect(jsonPath("$.data.rechargeAmount").value(100.00))
|
||||
.andExpect(jsonPath("$.data.rechargeUsageAmount").value(60.00));
|
||||
|
||||
// 验证 Service 调用
|
||||
verify(rechargeDetailsService, times(1)).getManageRechargeDetailsBalanceInfo();
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.cf.imes.module.system.service.funds.recharge;
|
||||
|
||||
import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;
|
||||
import com.cf.imes.module.system.controller.admin.funds.recharge.vo.RechargeDetailsBalanceInfoRespVO;
|
||||
import com.cf.imes.module.system.service.funds.purchase.PurchaseService;
|
||||
import com.cf.imes.module.system.service.pay.PayOrderService;
|
||||
import jakarta.annotation.Resource;
|
||||
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 static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Gqr
|
||||
* @since 2025/10/27 10:44
|
||||
*/
|
||||
@Import(RechargeDetailsServiceImpl.class)
|
||||
class RechargeDetailsServiceImplTest extends BaseDbUnitTest {
|
||||
@Resource
|
||||
private RechargeDetailsService detailsService;
|
||||
|
||||
@MockitoBean
|
||||
private PayOrderService payOrderService;
|
||||
|
||||
@MockitoBean
|
||||
private PurchaseService purchaseService;
|
||||
|
||||
@Test
|
||||
void testGetManageRechargeDetailsBalanceInfo() {
|
||||
// 模拟依赖返回值
|
||||
when(payOrderService.getRechargeTotalAmount()).thenReturn(new BigDecimal("200.00"));
|
||||
when(purchaseService.getTotalRechargeUsageAmount()).thenReturn(new BigDecimal("50.00"));
|
||||
|
||||
// 调用被测方法
|
||||
RechargeDetailsBalanceInfoRespVO result = detailsService.getManageRechargeDetailsBalanceInfo();
|
||||
|
||||
// 验证逻辑
|
||||
assertEquals(new BigDecimal("200.00"), result.getRechargeAmount());
|
||||
assertEquals(new BigDecimal("50.00"), result.getRechargeUsageAmount());
|
||||
verify(payOrderService).getRechargeTotalAmount();
|
||||
verify(purchaseService).getTotalRechargeUsageAmount();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user