
public class Air21QueryMileStoneJobTest{ @InjectMocks Air21QueryMileStoneJob air21QueryMileStoneJob ; @InjectMocks Air21OrderStatusHandler air21OrderStatusHandler; @org.mockito.Mock private LocationDAO locationDAO; @Before public void setup() { locationDAO= new LocationDAO(); air21OrderStatusHandler = new Air21OrderStatusHandler(); air21OrderStatusHandler.setLocationDAO(locationDAO); /*Map<String, String> criteriaMap = new HashMap<String, String>(); criteriaMap.put("location", "AA"); Mockito.when(locationDAO.query(criteriaMap)).thenReturn("GTM+8");*/ MockitoAnnotations.initMocks(this); } @Test public void testQueryActionDateTzName() throws Exception { Map<String, String> criteriaMap = new HashMap<String, String>(); criteriaMap.put("location", "AA"); ArrayList<LocationDO> arrayList = new ArrayList<LocationDO>(); LocationDO locationDO = new LocationDO();locationDO.setTimezone("AAAA"); arrayList.add(locationDO); Mockito.when(locationDAO.query(criteriaMap)).thenReturn(arrayList); // Air21OrderStatusHandler air21OrderStatusHandler = new Air21OrderStatusHandler(); String queryActionDateTz = air21OrderStatusHandler.queryActionDateTz("AA"); System.out.println(queryActionDateTz); }
元代码:

public class Air21OrderStatusHandler { /******************************** * 1.convert bean step1 get comment data step2 loop scan_details step3 convert * data to order_status step4 collect order_status * * 2.if status is DDL or DL1 ,update edi_history isOverMileStone="true" * * 3.delete old milestone info * * 4.save new milestone to orderstatus table ***************************************/ @Autowired private LocationDAO locationDAO; public String queryActionDateTz(String postLocation) { LOGGER.info("start get timezone by location"); String fTimeZone = ""; Map<String, String> criteriaMap = new HashMap<String, String>(); criteriaMap.put("location", postLocation); @SuppressWarnings("unchecked") List<LocationDO> locationDOlist = (List<LocationDO>) locationDAO.query(criteriaMap); if (!locationDOlist.isEmpty() && locationDOlist.size() > 0) { fTimeZone = locationDOlist.get(0).getTimezone(); } else { fTimeZone = MilestoneConstant.TIME_ZONE; } LOGGER.info("end get timezone by location"); LOGGER.info("TimeZone is :{}", fTimeZone); return fTimeZone; }
springboot 使用Mock
@RunWith(MockitoJUnitRunner.class) @Slf4j public class TestMilestoneController {
解决依赖注入:
@InjectMocks public MilestoneServiceImpl milestoneService; @Mock public MilestoneService milestoneServices; @Mock private MilestoneRepository milestoneRepository; @Mock @Qualifier("mongoTemplate") private MongoTemplate mongoTemplate; @Mock private BookingService bookingService;
@Service @Slf4j public class MilestoneServiceImpl implements MilestoneService { @Autowired private MilestoneRepository milestoneRepository; @Autowired @Qualifier("mongoTemplate") private MongoTemplate mongoTemplate; @Autowired private BookingService bookingService; @Autowired private OrderService orderService; @Autowired private CreateAndPushMilestoneHandler createAndPushMilestoneHandler; @Autowired private MilestoneExcelUploadHandler milestoneExcelUploadHandler;
分类:
mock
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?