Commit 8b6307e7 by Phillip Webb

Use BDD Mockito

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