单元测试 mock静态方法

@RunWith(PowerMockRunner.class)
@PrepareForTest({LabelLibService.class}) 
public class LabelLibServiceTest {


    @InjectMocks
    private LabelLibService         labelLibService;


    @Mock
    private LabelDao                mockLabelDao;


    @Mock
    private UserContext             mockUserContext;


    @Mock
    private LabelLevelDao labelLevelDao;
    @Mock
    private LhsAdminScmConfig lhsAdminScmConfig;


    @Mock
    private UserDao                 userDao;


    @Mock
    private LabelManageDao         labelManagerDao;
    




    @Mock
    private LabelWarnDao     labelWarnServiceDao;


    @Mock
    private IUserService            userService;


    private MockHttpServletRequest  request;


    private MockHttpServletResponse response;


    @BeforeClass
    public static void beforeClass() {
        // 初始化ResultJsonUtils.errorMsg
        // ResultJsonUtils.errorMsg = new HashMap<String, String>();
        // ResultJsonUtils.errorMsg.put("-1001", "添加失败");
        // ResultJsonUtils.errorMsg.put("-1002", "修改失败");
        PowerMockito.mockStatic(LhsAdminScmConfig.class);
    }


    @Before
    public void before() {


        RequestContextListener listener = new RequestContextListener();
        MockServletContext context = new MockServletContext();
        request = new MockHttpServletRequest(context);
        listener.requestInitialized(new ServletRequestEvent(context, request));
        MockHttpSession session = new MockHttpSession();
        request.setSession(session);
        UserInfo userSession = new UserInfo();
        userSession.setUserCode("17102097");


        userSession.setRoleAttribute(1);


        userSession.setLabelAreaIdString("00");


        UserContext.setUserSession(userSession);
    }


    @After
    public void after() {
    }


    /**
     * 标签字典分页查询--正常逻辑测试1
     * 
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @Test
    public void testLabelDict1() throws Exception {
        List<LabelInfo> labelInfoList = new ArrayList<LabelInfo>();
        LabelInfo labelInfo2 = new LabelInfo();
        labelInfo2.setActvCoverRate("2.5");
        labelInfoList.add(labelInfo2);


        // 动态mock
        Map<String, Object> params1 = new HashMap<String, Object>();
        when(mockLabelDao.getLabelInfoGoods(params1)).thenReturn(labelInfoList);
        when(mockLabelDao.getLabelInfoCount(params1)).thenReturn(10);


        // 初始化标签参数
        LabelInfo labelInfo = new LabelInfo();
        labelInfo.setLabelYu(1);
        labelInfo.setLabelFormat("1");


        // 初始化分页参数
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setPage(1);
        paginationInfo.setRow(5);


        // 参考注意点
        // Mockito不能mock静态、final、私有方法等,所以引入PowerMockito
        // 这里Tools.class不mock,直接覆盖
        // 这里mock ResultJsonUtils.class,直接覆盖
        // PowerMockito.mockStatic(ResultJsonUtils.class);


        PowerMockito.mockStatic(UserContext.class);


        labelLibService.labelDict(labelInfo, paginationInfo);


        // 参考注意点
        // 这里验证不写,不影响单元测试覆盖率
        // 动态验证
        // Map<String, Object> params1 = JSONObject.parseObject(
        // "{\"actvCoverRate\":0.0,\"labelCover\":0,\"coverRate\":0.0,\"startRow\":0,\"labelFormat\":\"1\",\"labelYu\":1,\"row\":5}",
        // Map.class);
        // verify(mockLabelDao).getLabelInfoGoods(params1);


        // 静态验证
        // PowerMockito.verifyStatic();
    }


    /**
     * 标签字典分页查询--正常逻辑测试2
     * 
     * @throws Exception
     */
    @Test
    public void testLabelDict2() throws Exception {
        List<LabelInfo> labelInfoList = new ArrayList<LabelInfo>();
        LabelInfo labelInfo2 = new LabelInfo();
        labelInfo2.setActvCoverRate("2.5");
        labelInfoList.add(labelInfo2);
        Map<String, Object> params1 = new HashMap<String, Object>();
        when(mockLabelDao.getLabelInfo(params1)).thenReturn(labelInfoList);
        when(mockLabelDao.getLabelInfoCount(params1)).thenReturn(10);


        // 初始化标签参数
        LabelInfo labelInfo = new LabelInfo();
        labelInfo.setLabelYu(2);
        labelInfo.setLabelFormat("1");


        // 初始化分页参数
        PaginationInfo paginationInfo = new PaginationInfo();
        paginationInfo.setPage(1);
        paginationInfo.setRow(5);


        PowerMockito.mockStatic(ResultJsonUtils.class);
        PowerMockito.mockStatic(UserContext.class);


        labelLibService.labelDict(labelInfo, paginationInfo);
    }


   


    /**
     * 各个标签域总数数据(1)
     * 
     * @throws Exception
     */
    @Test
    public void testGetLabelData() throws Exception {
        // 初始化参数
        Map<String, Object> params1 = new HashMap<String, Object>();
        params1.put("labelYu", 0);
        params1.put("startTime", "2017-1-1");
        params1.put("endTime", "2017-1-1");
        when(mockLabelDao.getLabelYuTotalCount(0)).thenReturn(1);
        when(mockLabelDao.getUserLabelYuTotalCount(params1)).thenReturn(1);
        PowerMockito.mockStatic(LabelLibService.class);   
        PowerMockito.when(LabelLibService.getProperty("user.label.using")).thenReturn("1");
        PowerMockito.when(LabelLibService.getProperty("goods.label.using")).thenReturn("1");      
        PowerMockito.when(LabelLibService.getProperty("business.label.using")).thenReturn("1");     
        PowerMockito.when(LabelLibService.getProperty("context.label.using")).thenReturn("1");
        //when(LhsAdminScmConfig.getInstance().getAllItems()).thenReturn(1);
        labelLibService.getLabelData(0, 0);
    }


    /**
     * 各个标签域总数数据(2)
     * 
     * @throws Exception
     */
    @Test
    public void testGetLabelData2() throws Exception {
        // 初始化参数
        Map<String, Object> params1 = new HashMap<String, Object>();
        params1.put("labelYu", 0);
        params1.put("startTime", "2017-1-1");
        params1.put("endTime", "2017-1-1");
        when(mockLabelDao.getLabelYuTotalCount(0)).thenReturn(1);
        when(mockLabelDao.getUserLabelYuTotalCount(params1)).thenReturn(1);
        PowerMockito.mockStatic(ResultJsonUtils.class);
        PowerMockito.mockStatic(LabelLibService.class); 
        //mock静态方法
        PowerMockito.when(LabelLibService.getProperty("user.label.using")).thenReturn("1");
        PowerMockito.when(LabelLibService.getProperty("goods.label.using")).thenReturn("1");      
        PowerMockito.when(LabelLibService.getProperty("business.label.using")).thenReturn("1");     
        PowerMockito.when(LabelLibService.getProperty("context.label.using")).thenReturn("1");
        labelLibService.getLabelData(1, 0);
        labelLibService.getLabelData(1, 3);
        labelLibService.getLabelData(1, 4);
        labelLibService.getLabelData(1, 5);
    }

猜你喜欢

转载自blog.csdn.net/weixin_35756522/article/details/80278209