zoukankan      html  css  js  c++  java
  • mock 的独立使用

    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);
        }
    View Code

    元代码:

    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;
        }
    View Code

    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;
  • 相关阅读:
    facade 对于有很多接口的提供一个简单的接口
    UML 常用符号 转
    <html> 按钮换行
    javascript: change the content of table
    [转载]当今计算机软件开发和应用领域最重要十种关键技术
    括号匹配算法
    求出给定两日期段 之间的交集算法
    表中的数据导出为insert语句的简单方法
    投色子下注的小程序
    未来社会发展趋势__P2p时代
  • 原文地址:https://www.cnblogs.com/lshan/p/9285320.html
Copyright © 2011-2022 走看看