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>