云
2026-10-27 ffca46b39d25439a06a27cafda2ec523a3fb67cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cn.iocoder.yudao.module.mes.service.wm.transaction;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.materialstock.MesWmMaterialStockDO;
import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum;
import cn.iocoder.yudao.module.mes.service.wm.materialstock.MesWmMaterialStockService;
import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
import java.math.BigDecimal;
 
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.WM_MATERIAL_STOCK_SELECTION_MISMATCH;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
 
@ExtendWith(MockitoExtension.class)
class MesWmTransactionReelSafetyTest {
    @InjectMocks private MesWmTransactionServiceImpl service;
    @Mock private MesWmMaterialStockService stockService;
 
    @Test
    void rejectsSelectedStockWithDifferentBatch() {
        when(stockService.getMaterialStock(1L)).thenReturn(new MesWmMaterialStockDO().setId(1L)
                .setItemId(2L).setWarehouseId(3L).setLocationId(4L).setAreaId(5L).setBatchId(6L));
        MesWmTransactionSaveReqDTO req = new MesWmTransactionSaveReqDTO()
                .setType(MesWmTransactionTypeEnum.OUT.getType()).setQuantity(BigDecimal.ONE.negate())
                .setItemId(2L).setWarehouseId(3L).setLocationId(4L).setAreaId(5L)
                .setBatchId(7L).setBatchCode("other-batch").setMaterialStockId(1L);
 
        ServiceException ex = assertThrows(ServiceException.class, () -> service.createTransaction(req));
        assertEquals(WM_MATERIAL_STOCK_SELECTION_MISMATCH.getCode(), ex.getCode());
        verify(stockService, never()).updateMaterialStockQuantity(anyLong(), any(), anyBoolean());
    }
}