`
chenjingbo
  • 浏览: 460004 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

PowerMockito example

 
阅读更多

package com.taobao.ju.c2b.facade.manager.impl;

import com.taobao.ju.common.manager.ManagerException;
import com.taobao.ump.client.meta.TargetDef;
import com.taobao.ump.client.result.ResultSupport;
import com.taobao.ump.core.service.client.MarketingActivityTopServiceClient;
import com.taobao.ump.marketing.*;
import com.taobao.ump.marketing.internal.InternalMarketingBuilder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Date;

@RunWith(PowerMockRunner.class)
@PrepareForTest({UmpDetailManagerImpl.class, InternalMarketingBuilder.class, MarketingActivityTopServiceClient.class})
public class UmpDetailManagerImplTest {
    
    private static final MarketingDetail marketingDetail = new MarketingDetail(){

        @Override
        public MarketingActivity getMarketingActivity() {
            return null;  
        }

        @Override
        public Long getDetailId() {
            return detailId;
        }

        @Override
        public Date getStartTime() {
            return null;  
        }

        @Override
        public Date getEndTime() {
            return null;  
        }

        @Override
        public DetailStatus getStatus() {
            return null;  
        }

        @Override
        public MarketingDetail setStartTime(Date startTime) {
            return null;  
        }

        @Override
        public MarketingDetail setEndTime(Date endTime) {
            return null;  
        }

        @Override
        public MarketingDetail setStatus(DetailStatus status) {
            return null;  
        }

        @Override
        public void setRange(ParticipateType participateType, Long participateId) {
            
        }

        @Override
        public ParticipateType getParticipateType() {
            return null;  
        }

        @Override
        public Long getParticipateId() {
            return null;  
        }

        @Override
        public void define(String paramId, Object value) {
            
        }

        @Override
        public void undefine(String paramId) {
            
        }

        @Override
        public Collection<ParameterValue> getMustDefineParameters() {
            return null;  
        }

        @Override
        public Collection<ParameterValue> getDefinedParameters() {
            return null;  
        }

        @Override
        public Collection<ParameterValue> getDefinableParameters() {
            return null;  
        }

        @Override
        public boolean isAllDefined() {
            return false;  
        }
    };
    
    private static final MarketingActivity marketingActivity = new MarketingActivity(){

        @Override
        public MarketingTool getMarketingTool() {
            return null;  
        }

        @Override
        public Long getActivityId() {
            return null;  
        }

        @Override
        public String getName() {
            return null;  
        }

        @Override
        public String getDescription() {
            return null;  
        }

        @Override
        public ActivityType getType() {
            return null;  
        }

        @Override
        public ParticipateRange getParticipateRange() {
            return null;  
        }

        @Override
        public Date getStartTime() {
            return null;  
        }

        @Override
        public Date getEndTime() {
            return null;  
        }

        @Override
        public ActivityStatus getStatus() {
            return null;  
        }

        @Override
        public MetaData<TargetDef> getTargetMeta() {
            return null;  
        }

        @Override
        public MarketingActivity setName(String name) {
            return null;  
        }

        @Override
        public MarketingActivity setDescription(String description) {
            return null;  
        }

        @Override
        public MarketingActivity setType(ActivityType type) {
            return null;  
        }

        @Override
        public MarketingActivity setParticipateRange(ParticipateRange participateRange) {
            return null;  
        }

        @Override
        public MarketingActivity setStartTime(Date startTime) {
            return null;  
        }

        @Override
        public MarketingActivity setEndTime(Date endTime) {
            return null;  
        }

        @Override
        public MarketingActivity setStatus(ActivityStatus status) {
            return null;  
        }

        @Override
        public Short getOptions() {
            return null;  
        }

        @Override
        public MarketingActivity setTargetMeta(MetaData<TargetDef> target) {
            return null;  
        }

        @Override
        public MarketingActivity setOneOffPromotion(boolean isOneOffPromotion) {
            return null;  
        }

        @Override
        public boolean isOneOffPromotion() {
            return false;  
        }

        @Override
        public MarketingActivity setMultipleLayerPromotion(boolean isMultipleLayerPromotion) {
            return null;  
        }

        @Override
        public boolean isMultipleLayerPromotion() {
            return false;  
        }

        @Override
        public void define(String paramId, Object value) {
            
        }

        @Override
        public void undefine(String paramId) {
            
        }

        @Override
        public Collection<ParameterValue> getMustDefineParameters() {
            return null;  
        }

        @Override
        public Collection<ParameterValue> getDefinedParameters() {
            return null;  
        }

        @Override
        public Collection<ParameterValue> getDefinableParameters() {
            return null;  
        }

        @Override
        public boolean isAllDefined() {
            return false;  
        }
    };

    private UmpDetailManagerImpl umpDetailManager = PowerMockito.spy(new UmpDetailManagerImpl());
    private InternalMarketingBuilder internalMarketingBuilder = PowerMockito.spy(new InternalMarketingBuilder());
    private MarketingActivityTopServiceClient marketingActivityTopServiceClient = PowerMockito.spy(new MarketingActivityTopServiceClient());
    
    private static final String content = "content";
    
    private static final Long activityId = 2012l,sellerId = 2013l,detailId = 2014l;

    @Before
    public void setUp() throws Exception{
        Field field = UmpDetailManagerImpl.class.getDeclaredField("internalMarketingBuilder");
        field.setAccessible(true);
        field.set(umpDetailManager,internalMarketingBuilder);
        field = UmpDetailManagerImpl.class.getDeclaredField("marketingActivityTopServiceClient");
        field.setAccessible(true);
        field.set(umpDetailManager,marketingActivityTopServiceClient);
    }

    @Test
    public void testCreateMarketingDetail() throws Exception {
        PowerMockito.doReturn(marketingDetail).when(internalMarketingBuilder,"createMarketingDetail",marketingActivity);
        Assert.assertTrue(umpDetailManager.createMarketingDetail(marketingActivity).getDetailId() == detailId);
    }

    @Test(expected = ManagerException.class)
    public void testCreateMarketingDetail_Exception() throws Exception {
        PowerMockito.doThrow(new RuntimeException("test")).when(internalMarketingBuilder, "createMarketingDetail", marketingActivity);
        Assert.assertTrue(umpDetailManager.createMarketingDetail(marketingActivity).getDetailId() == detailId);
    }

    @Test(expected = ManagerException.class)
    public void testCreateMarketingDetail_Exception2() throws Exception {
        umpDetailManager.createMarketingDetail(null);
    }

    @Test
    public void testAddMarketingDetail() throws Exception {
        PowerMockito.doReturn(content).when(internalMarketingBuilder,"build",marketingDetail);
        PowerMockito.doReturn(prepareResultSupport()).when(marketingActivityTopServiceClient,"addMarketingDetail",activityId,content,sellerId);
        Assert.assertEquals(umpDetailManager.addMarketingDetail(activityId,marketingDetail,sellerId).getDefaultModel(),detailId);
    }

    @Test(expected = ManagerException.class)
    public void testAddMarketingDetail_Exception() throws Exception {
        PowerMockito.doReturn(content).when(internalMarketingBuilder,"build",marketingDetail);
        PowerMockito.doThrow(new RuntimeException("test")).when(marketingActivityTopServiceClient,"addMarketingDetail",activityId,content,sellerId);
        umpDetailManager.addMarketingDetail(activityId,marketingDetail,sellerId);
    }

    @Test
    public void testDeleteMarketingDetail() throws Exception {
        PowerMockito.doReturn(prepareResultSupport2()).when(marketingActivityTopServiceClient,"deleteMarketingDetail",detailId,sellerId);
        Assert.assertFalse(umpDetailManager.deleteMarketingDetail(detailId,sellerId).isSuccess());
    }

    @Test(expected = ManagerException.class)
    public void testDeleteMarketingDetail_Exception() throws Exception {
        PowerMockito.doThrow(new RuntimeException("test")).when(marketingActivityTopServiceClient,"deleteMarketingDetail",detailId,sellerId);
        Assert.assertFalse(umpDetailManager.deleteMarketingDetail(detailId,sellerId).isSuccess());
    }

    @Test
    public void testUpdateMarketingActivityDetail() throws Exception {
        PowerMockito.doReturn(content).when(internalMarketingBuilder,"build",marketingDetail);
        PowerMockito.doReturn(prepareResultSupport2()).when(marketingActivityTopServiceClient,"updateMarketingDetail",detailId,content,sellerId);
        Assert.assertFalse(umpDetailManager.updateMarketingActivityDetail(detailId,marketingDetail,sellerId).isSuccess());
    }

    @Test(expected = ManagerException.class)
    public void testUpdateMarketingActivityDetail_Exception() throws Exception {
        PowerMockito.doReturn(content).when(internalMarketingBuilder,"build",marketingDetail);
        PowerMockito.doThrow(new RuntimeException("test")).when(marketingActivityTopServiceClient,"updateMarketingDetail",detailId,content,sellerId);
        Assert.assertFalse(umpDetailManager.updateMarketingActivityDetail(detailId,marketingDetail,sellerId).isSuccess());
    }
    
    private ResultSupport prepareResultSupport(){
        ResultSupport resultSupport = new ResultSupport(false);
        resultSupport.setDefaultModel(detailId);
        return resultSupport;
    }

    private ResultSupport prepareResultSupport2(){
        ResultSupport resultSupport = new ResultSupport(false);
        resultSupport.setDefaultModel(Boolean.TRUE);
        return resultSupport;
    }
}

 

<dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-common</artifactId>
            <version>1.4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-core</artifactId>
            <version>1.4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-reflect</artifactId>
            <version>1.4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-support</artifactId>
            <version>1.4.10</version>
            <scope>test</scope>
        </dependency>
 

 

分享到:
评论
2 楼 chenjingbo 2013-10-29  
luhantu 写道
这样的代码贴出来……没有参考价值啊……


是很不友好..自己做一个记录而已了.
1 楼 luhantu 2013-09-29  
这样的代码贴出来……没有参考价值啊……

相关推荐

    Hibernate中Example的用法

    ### Hibernate中Example的用法详解 #### 一、前言 Hibernate作为一款强大的ORM(对象关系映射)框架,提供了多种灵活的查询方式,其中`Example`查询是基于对象实例进行匹配查询的一种方法,它允许开发者通过提供一...

    Example_example软件_ABAQUSUMAT_源码

    本文将围绕"Example_example软件_ABAQUSUMAT_源码"这一主题,深入探讨UMAT的使用方法以及提供的几个实例。 UMAT是ABAQUS中的核心组成部分,它通过用户编写FORTRAN代码来定义材料的应力-应变关系。UMAT子程序包含五...

    Go By Example 中文版

    Go By Example 中文版 Go By Example 中文版 Go By Example 中文版

    逆向工程Example方法使用说明

    ### 逆向工程Example方法使用说明 #### 一、引言 在软件开发过程中,特别是在使用MyBatis框架处理数据库操作时,繁琐的手工编写Mapper文件可能会极大地降低开发效率。为了提高开发效率并减少错误,逆向工程成为了一...

    silvaco example.zip

    这个"silvaco example.zip"压缩包提供了丰富的示例,帮助用户深入理解Silvaco TCAD的功能和应用。每个示例都涵盖了不同的模型,通过详尽的注释和可视化图像,使得学习过程更为直观易懂。 首先,我们来看看"tcad_...

    MiniDao-example1.2

    MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2MiniDao-example1.2...

    VHDL例程源码 example_confi

    VHDL例程源码 example_confiVHDL例程源码 example_confiVHDL例程源码 example_confiVHDL例程源码 example_confiVHDL例程源码 example_confiVHDL例程源码 example_confiVHDL例程源码 example_confiVHDL例程源码 ...

    React Native By Example

    React Native By Example by Richard Kho English | 24 Apr. 2017 | ASIN: B01M31KB4Q | 414 Pages | AZW3 | 4.46 MB Key Features Work on native APIs and UI Elements using React Native Get the best of both...

    Django2-by-example-zh.pdf 中文版 20190129修订

    Django 2 by example 博客项目–基础 Django 2 by example 博客项目–增加高级功能 Django 2 by example 博客项目–扩展功能 Django 2 by example 社交网站项目–用户身份验证 Django 2 by example 社交网站项目–-...

    jni-example.zip

    以上命令的路径我写的是绝对路径:/complie2/wangwei/svn_8880_r95/android/android-4.2.1_r1/out/target/common/obj/APPS/jni_example_intermediates/classes 下面的com.example.jni_example.MainActivity ...

    ibatis Example使用

    根据提供的文件信息,我们可以从中提炼出与ibatis及其Example特性相关的知识点。以下是对这些知识点的详细阐述: ### 1. **ibatis简介** ibatis(现称为MyBatis)是一种支持普通SQL查询、存储过程及高级映射的优秀...

    Java开发example2

    example2 example2 example2 example2 example2

    example.com

    example.com

    SELinux-by-Example-Using-Security-Enhanced-Linux

    SELinux by example is the first complete, hands-on guide to using SELinux in production environments. Authored by three leading SELinux researchers and developers, it illuminates every facet of ...

    JavaEE源代码 Example_Struts

    JavaEE源代码 Example_StrutsJavaEE源代码 Example_StrutsJavaEE源代码 Example_StrutsJavaEE源代码 Example_StrutsJavaEE源代码 Example_StrutsJavaEE源代码 Example_StrutsJavaEE源代码 Example_StrutsJavaEE源...

    pv example simulink

    pv example for studing。光伏发电系统的并网,扰动法

    [pdf]Ext JS 6 By Example

    一本国外牛人写的关于 ExtJS 6 的电子书 [Ext JS 6 By Example]。这份资料在 PACKT上卖 35.99 刀的,讲的很详细,例子也比较简单,容易理解,现我准备利用工作之余翻译这份文档,为自己学习加深理解,也希望能帮助更...

    learning sas by example

    learning sas by example

    VTK示例文件 VTK_Example

    在使用VS编译 VTK 8.2.0 时顺手生成的VTK示例文件,大概有20余个吧,文件夹里已经包含依赖,可正常运行 VTK示例文件 VTK_Example

Global site tag (gtag.js) - Google Analytics