Commit d91655f3c48dc59a61243f106726a4cf2a1346b9
1 parent
7dd12a3e
feature(商城): 修改单元测试
Showing
1 changed file
with
111 additions
and
21 deletions
yudao-module-mall/yudao-module-product-biz/src/test/java/cn/iocoder/yudao/module/product/service/spu/ProductSpuServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.product.service.spu; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.date.DateUtil; | |
| 4 | +import cn.hutool.core.util.RandomUtil; | |
| 3 | 5 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 4 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 7 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 8 | +import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | |
| 6 | 9 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 7 | 10 | import cn.iocoder.yudao.module.product.controller.admin.property.vo.property.ProductPropertyRespVO; |
| 8 | 11 | import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueRespVO; |
| ... | ... | @@ -33,10 +36,12 @@ import java.util.*; |
| 33 | 36 | import java.util.stream.Collectors; |
| 34 | 37 | import java.util.stream.Stream; |
| 35 | 38 | |
| 39 | +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 36 | 40 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 37 | 41 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 38 | 42 | |
| 39 | 43 | // TODO @芋艿:review 下单元测试 |
| 44 | + | |
| 40 | 45 | /** |
| 41 | 46 | * {@link ProductSpuServiceImpl} 的单元测试类 |
| 42 | 47 | * |
| ... | ... | @@ -67,6 +72,13 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest { |
| 67 | 72 | @MockBean |
| 68 | 73 | private ProductPropertyValueService productPropertyValueService; |
| 69 | 74 | |
| 75 | + public String generateNo() { | |
| 76 | + return DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomInt(100000, 999999); | |
| 77 | + } | |
| 78 | + | |
| 79 | + public Long generateId() { | |
| 80 | + return RandomUtil.randomLong(100000, 999999); | |
| 81 | + } | |
| 70 | 82 | |
| 71 | 83 | @Test |
| 72 | 84 | public void testCreateSpu_success() { |
| ... | ... | @@ -206,35 +218,120 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest { |
| 206 | 218 | Assertions.assertIterableEquals(createReqVO, spuList); |
| 207 | 219 | } |
| 208 | 220 | |
| 209 | - // TODO @luowenfeng:单测要分情况;类似你这个,可以分 2 个单测;一个是有预存预警的;一个是没库存预警的; | |
| 210 | - // 然后,参考其它模块的 getPage 类型的方法的单测。 | |
| 211 | 221 | @Test |
| 212 | - void getSpuPage() { | |
| 222 | + void getSpuPage_alarmStock_empty() { | |
| 223 | + // 调用 | |
| 224 | + ProductSpuPageReqVO productSpuPageReqVO = new ProductSpuPageReqVO(); | |
| 225 | + productSpuPageReqVO.setAlarmStock(true); | |
| 226 | + | |
| 227 | + PageResult<ProductSpuRespVO> spuPage = productSpuService.getSpuPage(productSpuPageReqVO); | |
| 228 | + | |
| 229 | + PageResult<Object> result = PageResult.empty(); | |
| 230 | + Assertions.assertIterableEquals(result.getList(), spuPage.getList()); | |
| 231 | + Assertions.assertEquals(spuPage.getTotal(), result.getTotal()); | |
| 232 | + } | |
| 233 | + | |
| 234 | + @Test | |
| 235 | + void getSpuPage_alarmStock() { | |
| 236 | + // mock 数据 | |
| 237 | + Long brandId = generateId(); | |
| 238 | + Long categoryId = generateId(); | |
| 239 | + String code = generateNo(); | |
| 240 | + | |
| 213 | 241 | // 准备参数 |
| 214 | - ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class); | |
| 242 | + ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class, o->{ | |
| 243 | + o.setStatus(ProductSpuStatusEnum.ENABLE.getStatus()); | |
| 244 | + o.setTotalStock(500); | |
| 245 | + o.setMinPrice(1); | |
| 246 | + o.setMaxPrice(50); | |
| 247 | + o.setMarketPrice(25); | |
| 248 | + o.setSpecType(ProductSpuSpecTypeEnum.RECYCLE.getType()); | |
| 249 | + o.setBrandId(brandId); | |
| 250 | + o.setCategoryId(categoryId); | |
| 251 | + o.setClickCount(100); | |
| 252 | + o.setCode(code); | |
| 253 | + o.setDescription("测试商品"); | |
| 254 | + o.setPicUrls(new ArrayList<>()); | |
| 255 | + o.setName("测试"); | |
| 256 | + o.setSalesCount(100); | |
| 257 | + o.setSellPoint("超级加倍"); | |
| 258 | + o.setShowStock(true); | |
| 259 | + o.setVideoUrl(""); | |
| 260 | + }); | |
| 215 | 261 | productSpuMapper.insert(createReqVO); |
| 216 | 262 | |
| 217 | - ArrayList<ProductSkuDO> remindSpuIds = Lists.newArrayList( | |
| 218 | -// randomPojo(ProductSkuDO.class, o -> o.setSpuId(createReqVO.getId())), | |
| 219 | -// randomPojo(ProductSkuDO.class, o -> o.setSpuId(createReqVO.getId())) | |
| 220 | - ); | |
| 263 | + Set<Long> alarmStockSpuIds = SetUtils.asSet(createReqVO.getId()); | |
| 264 | + | |
| 265 | + List<ProductSkuDO> productSpuDOS = Arrays.asList(randomPojo(ProductSkuDO.class, o -> { | |
| 266 | + o.setSpuId(createReqVO.getId()); | |
| 267 | + }), randomPojo(ProductSkuDO.class, o -> { | |
| 268 | + o.setSpuId(createReqVO.getId()); | |
| 269 | + })); | |
| 221 | 270 | |
| 222 | -// Mockito.when(productSkuService.getRemindSpuIds()).thenReturn(remindSpuIds); | |
| 271 | + Mockito.when(productSkuService.getSkusByAlarmStock()).thenReturn(productSpuDOS); | |
| 223 | 272 | |
| 224 | 273 | // 调用 |
| 225 | 274 | ProductSpuPageReqVO productSpuPageReqVO = new ProductSpuPageReqVO(); |
| 226 | -// productSpuPageReqVO.setTabStatus(2); | |
| 227 | - | |
| 275 | + productSpuPageReqVO.setAlarmStock(true); | |
| 228 | 276 | PageResult<ProductSpuRespVO> spuPage = productSpuService.getSpuPage(productSpuPageReqVO); |
| 229 | 277 | |
| 230 | - Set<Long> resultRemindSpuIds = new HashSet<>(); | |
| 231 | - resultRemindSpuIds.add(null); | |
| 232 | - PageResult<ProductSpuRespVO> result = ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(productSpuPageReqVO, resultRemindSpuIds)); | |
| 278 | + PageResult<ProductSpuRespVO> result = ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(productSpuPageReqVO, alarmStockSpuIds)); | |
| 233 | 279 | Assertions.assertIterableEquals(result.getList(), spuPage.getList()); |
| 234 | 280 | Assertions.assertEquals(spuPage.getTotal(), result.getTotal()); |
| 235 | 281 | } |
| 236 | 282 | |
| 237 | 283 | @Test |
| 284 | + void getSpuPage() { | |
| 285 | + // mock 数据 | |
| 286 | + Long brandId = generateId(); | |
| 287 | + Long categoryId = generateId(); | |
| 288 | + | |
| 289 | + // 准备参数 | |
| 290 | + ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class, o->{ | |
| 291 | + o.setStatus(ProductSpuStatusEnum.ENABLE.getStatus()); | |
| 292 | + o.setTotalStock(1); | |
| 293 | + o.setMinPrice(1); | |
| 294 | + o.setMaxPrice(1); | |
| 295 | + o.setMarketPrice(1); | |
| 296 | + o.setSpecType(ProductSpuSpecTypeEnum.RECYCLE.getType()); | |
| 297 | + o.setBrandId(brandId); | |
| 298 | + o.setCategoryId(categoryId); | |
| 299 | + o.setClickCount(1); | |
| 300 | + o.setCode(generateNo()); | |
| 301 | + o.setDescription("测试商品"); | |
| 302 | + o.setPicUrls(new ArrayList<>()); | |
| 303 | + o.setName("测试"); | |
| 304 | + o.setSalesCount(1); | |
| 305 | + o.setSellPoint("卖点"); | |
| 306 | + o.setShowStock(true); | |
| 307 | + }); | |
| 308 | + | |
| 309 | + // 准备参数 | |
| 310 | + productSpuMapper.insert(createReqVO); | |
| 311 | + // 测试 status 不匹配 | |
| 312 | + productSpuMapper.insert(cloneIgnoreId(createReqVO, o -> o.setStatus(ProductSpuStatusEnum.DISABLE.getStatus()))); | |
| 313 | + productSpuMapper.insert(cloneIgnoreId(createReqVO, o -> o.setStatus(ProductSpuStatusEnum.RECYCLE.getStatus()))); | |
| 314 | + // 测试 SpecType 不匹配 | |
| 315 | + productSpuMapper.insert(cloneIgnoreId(createReqVO, o -> o.setSpecType(ProductSpuSpecTypeEnum.DISABLE.getType()))); | |
| 316 | + // 测试 BrandId 不匹配 | |
| 317 | + productSpuMapper.insert(cloneIgnoreId(createReqVO, o -> o.setBrandId(generateId()))); | |
| 318 | + // 测试 CategoryId 不匹配 | |
| 319 | + productSpuMapper.insert(cloneIgnoreId(createReqVO, o -> o.setCategoryId(generateId()))); | |
| 320 | + | |
| 321 | + // 调用 | |
| 322 | + ProductSpuPageReqVO productSpuPageReqVO = new ProductSpuPageReqVO(); | |
| 323 | + productSpuPageReqVO.setAlarmStock(false); | |
| 324 | + productSpuPageReqVO.setBrandId(brandId); | |
| 325 | + productSpuPageReqVO.setStatus(ProductSpuStatusEnum.ENABLE.getStatus()); | |
| 326 | + productSpuPageReqVO.setCategoryId(categoryId); | |
| 327 | + | |
| 328 | + PageResult<ProductSpuRespVO> spuPage = productSpuService.getSpuPage(productSpuPageReqVO); | |
| 329 | + | |
| 330 | + PageResult<ProductSpuRespVO> result = ProductSpuConvert.INSTANCE.convertPage(productSpuMapper.selectPage(productSpuPageReqVO, (Set<Long>) null)); | |
| 331 | + Assertions.assertEquals(result, spuPage); | |
| 332 | + } | |
| 333 | + | |
| 334 | + @Test | |
| 238 | 335 | void testGetSpuPage() { |
| 239 | 336 | // 准备参数 |
| 240 | 337 | ProductSpuDO createReqVO = randomPojo(ProductSpuDO.class, o -> { |
| ... | ... | @@ -242,13 +339,6 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest { |
| 242 | 339 | }); |
| 243 | 340 | productSpuMapper.insert(createReqVO); |
| 244 | 341 | |
| 245 | - ArrayList<ProductSkuDO> remindSpuIds = Lists.newArrayList( | |
| 246 | -// randomPojo(ProductSkuDO.class, o -> o.setSpuId(createReqVO.getId())), | |
| 247 | -// randomPojo(ProductSkuDO.class, o -> o.setSpuId(createReqVO.getId())) | |
| 248 | - ); | |
| 249 | - | |
| 250 | -// Mockito.when(productSkuService.getRemindSpuIds()).thenReturn(remindSpuIds); | |
| 251 | - | |
| 252 | 342 | // 调用 |
| 253 | 343 | AppSpuPageReqVO appSpuPageReqVO = new AppSpuPageReqVO(); |
| 254 | 344 | appSpuPageReqVO.setCategoryId(2L); | ... | ... |