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());
}
}