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;
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值