首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

mockito mock void 步骤(mark帖)

2012-12-20 
mockito mock void 方法(mark帖)package com.taobao.ju.c2b.facade.manager.implimport com.taobao.ju.c2

mockito mock void 方法(mark帖)

package com.taobao.ju.c2b.facade.manager.impl;import com.taobao.ju.c2b.facade.domain.CommentQuery;import com.taobao.ju.c2b.follow.domain.CommentBO;import com.taobao.ju.common.manager.ManagerException;import com.taobao.matrix.comment.domain.*;import com.taobao.matrix.comment.enumconstants.ContentType;import com.taobao.matrix.comment.enumconstants.EnumPubComStatus;import com.taobao.matrix.comment.service.SnsCommentCoreService;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.InjectMocks;import org.mockito.Matchers;import org.mockito.Mock;import org.mockito.Mockito;import org.mockito.runners.MockitoJUnitRunner;import java.util.Arrays;import static org.mockito.Matchers.eq;import static org.mockito.Mockito.when;import static org.mockito.Matchers.any;/** * User: zhenghui * Emal: zhenghui.cjb@taobao.com * Date: 9/25/12  1:13 PM * */@RunWith(MockitoJUnitRunner.class)public class CommentManagerImplTest {    @InjectMocks    private static CommentManagerImpl commentManager = new CommentManagerImpl();    @Mock    private SnsCommentCoreService snsCommentCoreService;    private static final int typeID = 2012;        private static final int totoleNum = 2013;        private static final int readcount = 2014;        private static final long userId = 2015l;    static {        commentManager.setC2bTypeId(typeID);    }    @Test    public void testGetC2bTypeId() throws Exception {        Assert.assertTrue(commentManager.getC2bTypeId() == typeID);    }    @Test    public void testQueryComment4User() throws Exception {        Mockito.when(snsCommentCoreService.queryCommentToMe(any(MyCommentListQuery.class))).thenReturn(prepareQuery());        CommentQuery query = commentManager.queryComment4User(new CommentQuery());        Assert.assertTrue(query.getTotalItem() == totoleNum);        Assert.assertTrue(query.getJucCommentVOs().size() == 1);    }    @Test(expected = ManagerException.class)    public void testQueryComment4User_Exception() throws Exception {        when(snsCommentCoreService.queryCommentToMe(Matchers.any(MyCommentListQuery.class))).thenThrow(new RuntimeException("test"));        commentManager.queryComment4User(new CommentQuery());    }    @Test    public void testGetNotReadCommentCount() throws Exception {        Mockito.when(snsCommentCoreService.queryCommentNotReadCount(eq(userId),Matchers.eq(typeID),Matchers.any(ContentType.class))).thenReturn(preprareUnReadCommentResult());        int count = commentManager.getNotReadCommentCount(userId,ContentType.TOTAL);        Assert.assertTrue(count == readcount);    }    @Test(expected = ManagerException.class)    public void testGetNotReadCommentCount_Exception() throws Exception {        Mockito.when(snsCommentCoreService.queryCommentNotReadCount(eq(userId),Matchers.eq(typeID),Matchers.any(ContentType.class))).thenThrow(new RuntimeException("test"));        commentManager.getNotReadCommentCount(userId, ContentType.TOTAL);    }    @Test    public void testPublishComments() throws Exception {        when(snsCommentCoreService.publishComment(any(PublishCommentParam.class))).thenReturn(preparePublishCommentResult(true));        CommentBO commentBO = new CommentBO();        commentBO.setUserId(userId);        boolean success = commentManager.publishComments(commentBO);        Assert.assertTrue(success);        when(snsCommentCoreService.publishComment(any(PublishCommentParam.class))).thenReturn(preparePublishCommentResult(false));        success = commentManager.publishComments(commentBO);        Assert.assertFalse(success);    }    @Test(expected = ManagerException.class)    public void testPublishComments_Exception() throws Exception {        when(snsCommentCoreService.publishComment(any(PublishCommentParam.class))).thenThrow(new RuntimeException("test"));        CommentBO commentBO = new CommentBO();        commentBO.setUserId(userId);        commentManager.publishComments(commentBO);    }    @Test    public void testClearCommentNotReadCount() throws Exception {        commentManager.clearCommentNotReadCount(userId,ContentType.COMMENT);    }    @Test(expected = ManagerException.class)    public void testClearCommentNotReadCount_Exception() throws Exception {//        when(snsCommentCoreService.clearCommentNotReadCount(eq(userId),eq(typeID),any(ContentType.class))).thenThrow(new RuntimeException("test"));        Mockito.doThrow(new RuntimeException("test")).when(snsCommentCoreService).clearCommentNotReadCount(userId,typeID,ContentType.COMMENT);        commentManager.clearCommentNotReadCount(userId,ContentType.COMMENT);    }    private MyCommentListQuery prepareQuery(){        MyCommentListQuery query = new MyCommentListQuery();        query.setTotalItem(totoleNum);        MyCommentInfoQuery mciq = new MyCommentInfoQuery();        query.setMyCommentList(Arrays.asList(mciq));        return query;    }        private UnReadCommentResult preprareUnReadCommentResult(){        UnReadCommentResult result = new UnReadCommentResult();        result.setTotalCommentUnReadCount(readcount);        return result;    }    private PublishCommentResult preparePublishCommentResult(boolean success){        PublishCommentResult result = new PublishCommentResult();        if(success){            result.setStatus(EnumPubComStatus.SUCCESS);        } else {            result.setStatus(EnumPubComStatus.EMPTY);        }        return result;    }}
?

热点排行