zoukankan      html  css  js  c++  java
  • powermock单元测试小结

      最近时不时的需要单元测试来写覆盖率。简单总结一下日常心得:

    1.首先指明需要测试的类:@PrepareForTest({ RewardGoldServiceImpl.class })

    2.其次在测试类中new出被测试对象:@InjectMocks
                                                              RewardGoldServiceImpl rewardGoldService = new RewardGoldServiceImpl(); 

    3.然后在遇到所需测试类中的dao方法时候,进行mock:@Mock
                                                                                         IDictionaryDao dictionaryDao;

                                                                      进行预加载类:@Before
                                                                                          public void init() {
                                                                                               MockitoAnnotations.initMocks(this);
                                                                                          }

    4.最后编写测试类进行测试,主体代码如下:

    @PrepareForTest({ RewardGoldServiceImpl.class })
    public class RewardGoldServiceImplTest {
        // 被测对象
        @InjectMocks
        RewardGoldServiceImpl rewardGoldService = new RewardGoldServiceImpl(); 
        /**
         * 字典表操作
         */
        @Mock
        IDictionaryDao dictionaryDao;
        
        @Mock
        IRewardGoldDao rewardGoldDao;
        
        @Before
        public void init() {
            MockitoAnnotations.initMocks(this);
        }
        
        @Test
        public void testGetSystemMoney(){
            String accountPeriod = "201812";
            String payMerchantCode = "7236402";
            List<DictionaryPo> dictionaryList = new ArrayList<DictionaryPo>();
            DictionaryPo dictionaryPo = new DictionaryPo();
            PowerMockito.when(dictionaryDao.getDictionaryByFieldName("freightBonusProductCode")).thenReturn(dictionaryList);
            dictionaryPo.setFieldID(1);
            dictionaryPo.setFieldName("111");
            dictionaryPo.setFieldValue("123");
            dictionaryList.add(dictionaryPo);
             
            rewardGoldService.getSystemMoney(accountPeriod, payMerchantCode);
            
        }
        
    
    }
  • 相关阅读:
    如何用PostMan
    LINQ笔记-LINQ操作DataTable
    EF Core利用Transaction对数据进行回滚保护
    php 替换模板中的 PHP源码标签字符方法
    php读取文件使用redis的pipeline(管道)导入大批量数据
    Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程
    php 版本升高后 会出现 之Deprecated: Function ereg_replace() is deprecated的解决方法
    IIS + FastCGI+php(从5.2升级到5.3)
    Nginx 出现 _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/
    nginx 环境不支持thinkPHP
  • 原文地址:https://www.cnblogs.com/wangzhengyu/p/10482299.html
Copyright © 2011-2022 走看看