mock片断2 使用powermock针对private或static写单元测试

6 篇文章 0 订阅
4 篇文章 0 订阅
/* 需要测试的类  */
public class DemoServiceFactoryImpl implements DemoServiceFactory {

    private static final int Rerty_times = 3;
    private final DemoConfigtasConfig;

    public DemoServiceFactoryImpl(String tasConfigEnum) {
        this.DemoConfig= new TASConfig(tasConfigEnum);
    }

    public DemoServiceFactoryImpl(DemoConfigtasConfig) {
        this.DemoConfig= tasConfig;
    }

    @Override
    public StreamingTradeUpdateService createStreamingTradeUpdateServiceProxy() {
        Supplier<StreamingTradeUpdateService> supplier = () -> ServiceFactory.createSSLStreamingTradeUpdateServiceProxy(tasConfig.runtimeEnvironment);
        return RetryUtil.retryUntil(supplier, Rerty_times);
    }


    @Override
    public StreamingDemoQueryService createStreamingDemoQueryServiceProxy() {

        return RetryUtil.retryUntil(() -> ServiceFactory.createSSLStreamingDemoQueryServiceProxy(tasConfig.runtimeEnvironment), Rerty_times);
    }
}



import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ServiceFactory.class})
public class DemoServiceFactoryTest {

    private DemoServiceFactoryImpl serviceFactory;
    private DemoConfigconfig;
    private RuntimeException exception;
    @Mock
    private StreamingTradeUpdateService streamingUpdateService;
    @Mock
    private StreamingDemoQueryService streamingDemoQueryService;

    @Before
    public void setUp() throws Exception {
        config = new TASConfig("SIT");
        serviceFactory = new DemoServiceFactoryImpl(config);
        exception = new RuntimeException("mock exception");
    }

    @Test
    public void canCreateStreamingUpdateServiceWithoutRetry() throws Exception {

        PowerMockito.mockStatic(ServiceFactory.class);
        given(ServiceFactory.createSSLStreamingTradeUpdateServiceProxy(config.runtimeEnvironment)).willReturn(streamingUpdateService);
        StreamingTradeUpdateService actualStreamUpdateService = serviceFactory.createStreamingTradeUpdateServiceProxy();
        assertThat(actualStreamUpdateService, is(streamingUpdateService));
        PowerMockito.verifyStatic(times(1));
        ServiceFactory.createSSLStreamingTradeUpdateServiceProxy(any());


    }

    @Test
    public void canCreateStreamingUpdateServiceByRetry3Times() throws Exception {
        PowerMockito.mockStatic(ServiceFactory.class);
        given(ServiceFactory.createSSLStreamingTradeUpdateServiceProxy(config.runtimeEnvironment))
                .willThrow(exception).willThrow(exception).willReturn(streamingUpdateService);
        StreamingTradeUpdateService actualStreamUpdateService = serviceFactory.createStreamingTradeUpdateServiceProxy();
        assertThat(actualStreamUpdateService, is(streamingUpdateService));
        PowerMockito.verifyStatic(times(3));
        ServiceFactory.createSSLStreamingTradeUpdateServiceProxy(any());

    }

    @Test
    public void willFailCreateStreamingUpdateServiceByRetryAfterRetry3Times() throws Exception {
        PowerMockito.mockStatic(ServiceFactory.class);
        given(ServiceFactory.createSSLStreamingTradeUpdateServiceProxy(config.runtimeEnvironment)).willThrow(exception).willThrow(exception).willThrow(exception);
        Exception actualException = null;
        try {
            serviceFactory.createStreamingTradeUpdateServiceProxy();
        } catch (Exception e) {
            actualException = e;
        }
        assertNotNull(actualException);
        PowerMockito.verifyStatic(times(3));
        ServiceFactory.createSSLStreamingTradeUpdateServiceProxy(any());
    }

    @Test
    public void canCreateStreamingQueryServiceWithoutRetry() throws Exception {
        PowerMockito.mockStatic(ServiceFactory.class);
        given(ServiceFactory.createSSLStreamingDemoQueryServiceProxy(config.runtimeEnvironment)).willReturn(streamingDemoQueryService);
        StreamingDemoQueryService teststreamingDemoQueryService = serviceFactory.createStreamingDemoQueryServiceProxy();
        assertThat(teststreamingDemoQueryService, is(streamingDemoQueryService));
        PowerMockito.verifyStatic(times(1));
    }

    @Test
    public void canCreateStreamingQueryServiceByRetry3Times() throws Exception {
        PowerMockito.mockStatic(ServiceFactory.class);
        given(ServiceFactory.createSSLStreamingDemoQueryServiceProxy(config.runtimeEnvironment)).willThrow(exception).willThrow(exception).willReturn(streamingDemoQueryService);
        StreamingDemoQueryService teststreamingDemoQueryService = serviceFactory.createStreamingDemoQueryServiceProxy();
        assertThat(teststreamingDemoQueryService, is(streamingDemoQueryService));
        PowerMockito.verifyStatic(times(3));
        ServiceFactory.createSSLStreamingDemoQueryServiceProxy(any());
    }

    @Test
    public void willFailCreateStreamingQueryServiceAfterRetry3Times() throws Exception {
        PowerMockito.mockStatic(ServiceFactory.class);
        given(ServiceFactory.createSSLStreamingDemoQueryServiceProxy(config.runtimeEnvironment)).willThrow(exception).willThrow(exception).willThrow(exception);
        Exception actualException = null;
        try{
            serviceFactory.createStreamingDemoQueryServiceProxy();
        }catch (Exception e){
            actualException = e;
        }
        assertNotNull(actualException);
        PowerMockito.verifyStatic(times(3));
        ServiceFactory.createSSLStreamingDemoQueryServiceProxy(any());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值