Use BDD Mockito
This commit is contained in:
@@ -28,6 +28,7 @@ import com.netflix.appinfo.InstanceInfo;
|
||||
import com.netflix.discovery.DiscoveryClient;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -61,8 +62,8 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void onWhenRequested() throws Exception {
|
||||
Mockito.when(this.client.getNextServerFromEureka("CONFIGSERVER", false))
|
||||
.thenReturn(this.info);
|
||||
given(this.client.getNextServerFromEureka("CONFIGSERVER", false)).willReturn(
|
||||
this.info);
|
||||
setup("spring.cloud.config.discovery.enabled=true");
|
||||
assertEquals(
|
||||
1,
|
||||
@@ -77,8 +78,8 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
|
||||
@Test
|
||||
public void setsPasssword() throws Exception {
|
||||
this.info.getMetadata().put("password", "bar");
|
||||
Mockito.when(this.client.getNextServerFromEureka("CONFIGSERVER", false))
|
||||
.thenReturn(this.info);
|
||||
given(this.client.getNextServerFromEureka("CONFIGSERVER", false)).willReturn(
|
||||
this.info);
|
||||
setup("spring.cloud.config.discovery.enabled=true");
|
||||
ConfigClientProperties locator = this.context
|
||||
.getBean(ConfigClientProperties.class);
|
||||
@@ -90,8 +91,8 @@ public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
|
||||
@Test
|
||||
public void setsPath() throws Exception {
|
||||
this.info.getMetadata().put("configPath", "/bar");
|
||||
Mockito.when(this.client.getNextServerFromEureka("CONFIGSERVER", false))
|
||||
.thenReturn(this.info);
|
||||
given(this.client.getNextServerFromEureka("CONFIGSERVER", false)).willReturn(
|
||||
this.info);
|
||||
setup("spring.cloud.config.discovery.enabled=true");
|
||||
ConfigClientProperties locator = this.context
|
||||
.getBean(ConfigClientProperties.class);
|
||||
|
||||
@@ -39,9 +39,9 @@ import com.netflix.loadbalancer.Server;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -66,16 +66,13 @@ public class RibbonInterceptorTests {
|
||||
public void testIntercept() throws Exception {
|
||||
RibbonServer server = new RibbonServer("myservice", new Server("myhost", 8080));
|
||||
RibbonInterceptor interceptor = new RibbonInterceptor(new MyClient(server));
|
||||
|
||||
when(this.request.getURI()).thenReturn(new URL("http://myservice").toURI());
|
||||
when(this.execution.execute(isA(HttpRequest.class), isA(byte[].class)))
|
||||
.thenReturn(this.response);
|
||||
given(this.request.getURI()).willReturn(new URL("http://myservice").toURI());
|
||||
given(this.execution.execute(isA(HttpRequest.class), isA(byte[].class)))
|
||||
.willReturn(this.response);
|
||||
ArgumentCaptor<HttpRequestWrapper> argument = ArgumentCaptor
|
||||
.forClass(HttpRequestWrapper.class);
|
||||
|
||||
ClientHttpResponse response = interceptor.intercept(this.request, new byte[0],
|
||||
this.execution);
|
||||
|
||||
assertNotNull("response was null", response);
|
||||
verify(this.execution).execute(argument.capture(), isA(byte[].class));
|
||||
HttpRequestWrapper wrapper = argument.getValue();
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.net.URL;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
|
||||
@@ -36,10 +35,10 @@ import com.netflix.loadbalancer.ServerStats;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.anyDouble;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -61,7 +60,7 @@ public class RibbonLoadBalancerClientTests {
|
||||
@Before
|
||||
public void init() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
Mockito.when(this.clientFactory.getLoadBalancerContext(anyString())).thenReturn(
|
||||
given(this.clientFactory.getLoadBalancerContext(anyString())).willReturn(
|
||||
new RibbonLoadBalancerContext(this.loadBalancer));
|
||||
}
|
||||
|
||||
@@ -144,13 +143,15 @@ public class RibbonLoadBalancerClientTests {
|
||||
|
||||
protected RibbonLoadBalancerClient getRibbonLoadBalancerClient(
|
||||
RibbonServer ribbonServer) {
|
||||
when(this.loadBalancer.getName()).thenReturn(ribbonServer.getServiceId());
|
||||
when(this.loadBalancer.chooseServer(anyString())).thenReturn(ribbonServer.server);
|
||||
when(this.loadBalancer.getLoadBalancerStats()).thenReturn(this.loadBalancerStats);
|
||||
when(this.loadBalancerStats.getSingleServerStat(ribbonServer.server)).thenReturn(
|
||||
this.serverStats);
|
||||
when(this.clientFactory.getLoadBalancer(this.loadBalancer.getName())).thenReturn(
|
||||
this.loadBalancer);
|
||||
given(this.loadBalancer.getName()).willReturn(ribbonServer.getServiceId());
|
||||
given(this.loadBalancer.chooseServer(anyString()))
|
||||
.willReturn(ribbonServer.server);
|
||||
given(this.loadBalancer.getLoadBalancerStats())
|
||||
.willReturn(this.loadBalancerStats);
|
||||
given(this.loadBalancerStats.getSingleServerStat(ribbonServer.server))
|
||||
.willReturn(this.serverStats);
|
||||
given(this.clientFactory.getLoadBalancer(this.loadBalancer.getName()))
|
||||
.willReturn(this.loadBalancer);
|
||||
return new RibbonLoadBalancerClient(this.clientFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -102,15 +102,15 @@ public class DomainExtractingServerListTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
ServerList<Server> originalServerList = mock(ServerList.class);
|
||||
InstanceInfo instanceInfo = mock(InstanceInfo.class);
|
||||
when(server.getInstanceInfo()).thenReturn(instanceInfo);
|
||||
when(server.getHost()).thenReturn(HOST_NAME);
|
||||
when(instanceInfo.getMetadata()).thenReturn(
|
||||
given(server.getInstanceInfo()).willReturn(instanceInfo);
|
||||
given(server.getHost()).willReturn(HOST_NAME);
|
||||
given(instanceInfo.getMetadata()).willReturn(
|
||||
ImmutableMap.<String, String> builder().put("instanceId", INSTANCE_ID)
|
||||
.build());
|
||||
when(instanceInfo.getHostName()).thenReturn(HOST_NAME);
|
||||
when(instanceInfo.getIPAddr()).thenReturn(IP_ADDR);
|
||||
when(instanceInfo.getPort()).thenReturn(PORT);
|
||||
when(originalServerList.getInitialListOfServers()).thenReturn(
|
||||
given(instanceInfo.getHostName()).willReturn(HOST_NAME);
|
||||
given(instanceInfo.getIPAddr()).willReturn(IP_ADDR);
|
||||
given(instanceInfo.getPort()).willReturn(PORT);
|
||||
given(originalServerList.getInitialListOfServers()).willReturn(
|
||||
Arrays.<Server> asList(server));
|
||||
return new DomainExtractingServerList(originalServerList, config,
|
||||
approximateZoneFromHostname);
|
||||
|
||||
@@ -32,7 +32,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
/**
|
||||
@@ -203,9 +203,8 @@ public class ProxyRouteLocatorTests {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Lists.newArrayList(IGNOREDSERVICE));
|
||||
|
||||
when(this.discovery.getServices()).thenReturn(Lists.newArrayList(IGNOREDSERVICE));
|
||||
|
||||
given(this.discovery.getServices())
|
||||
.willReturn(Lists.newArrayList(IGNOREDSERVICE));
|
||||
Map<String, String> routesMap = routeLocator.getRoutes();
|
||||
String serviceId = routesMap.get(getMapping(IGNOREDSERVICE));
|
||||
assertNull("routes did not ignore " + IGNOREDSERVICE, serviceId);
|
||||
@@ -215,7 +214,7 @@ public class ProxyRouteLocatorTests {
|
||||
public void testAutoRoutes() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
|
||||
this.properties);
|
||||
when(this.discovery.getServices()).thenReturn(Lists.newArrayList(MYSERVICE));
|
||||
given(this.discovery.getServices()).willReturn(Lists.newArrayList(MYSERVICE));
|
||||
Map<String, String> routesMap = routeLocator.getRoutes();
|
||||
assertNotNull("routesMap was null", routesMap);
|
||||
assertFalse("routesMap was empty", routesMap.isEmpty());
|
||||
@@ -229,7 +228,7 @@ public class ProxyRouteLocatorTests {
|
||||
this.properties.getRoutes().put(MYSERVICE, route);
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
|
||||
this.properties);
|
||||
when(this.discovery.getServices()).thenReturn(Lists.newArrayList(MYSERVICE));
|
||||
given(this.discovery.getServices()).willReturn(Lists.newArrayList(MYSERVICE));
|
||||
Map<String, String> routesMap = routeLocator.getRoutes();
|
||||
assertNotNull("routesMap was null", routesMap);
|
||||
assertFalse("routesMap was empty", routesMap.isEmpty());
|
||||
|
||||
Reference in New Issue
Block a user