The Mock Anything Toolkit for Java--JMockit

           

                                      The Mock Anything Toolkit for Java--JMockit

  特征1.满足常规mock框架的功能

         2.mock static 以及final 的类和方法。

         3.支持依赖注入

  下面看代码例子:

   

@Tested
    private DistributorService distributorService;

    @Mocked
    private DistributorDao distributordao;
    @Mocked
    private DistributorAndAreaRelationDao relationDao;

@Test
    public void edit(@Mocked final IConnection conn,@Mocked final CacheService cacheService) {

        String json = "{\"id\":1,\"num\":\"120020\",\"name\":\"马云2\",\"remark\":\"随便写点\",\"area\":[{\"pv\":\"110000\",\"t\":1},{\"pv\":\"110000\",\"city\":\"11011\",\"t\":2},{\"pv\":\"120000\",\"city\":\"120100\",\"t\":1}],\"header\":{\"uid\":\"aaaaaa\",\"cid\":1,\"clid\":\"Zhabei\",\"uip\":\"aaaaaaaaaa\",\"rid\":\"6ba678cb-b2a5-426a-ad86-bf491aed172e\"}}";
        DistributorRequest distributorRequest = JSON.parseObject(json.toString(), DistributorRequest.class);

        try {
            new Expectations() {
                {
                    distributordao.checkUniqueDistributorNumber(conn, (Distributor) any, true);
                    result = false;
                    distributordao.editDistributor(conn, (Distributor)any);
                    result=true;
                    relationDao.save(conn,(DistributorAndAreaRelation)any);
                    result=100;
                    relationDao.delete(conn, anyString, anyInt);
                    result=true;

                }
            };
            new MockUp<SystemInitialization>() {
                @Mock
                public CacheService getCache() {
                    return cacheService;
                }
            };
            new MockUp<ConnectionManager>() {
                @Mock
                public IConnection getConnectionOpenTransaction() {
                    return conn;
                }
            };

            distributorService.edit(distributorRequest);
            Assert.assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail("DistributorServiceTest edit mock handle failed!");
        }
    }

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值