Replace EasyMock with Mockito
Change-Id: I672a1b237778d766204e32dda14cfe00a6d3cca9
diff --git a/BUCK b/BUCK
index d6f1201..523c272 100644
--- a/BUCK
+++ b/BUCK
@@ -15,6 +15,7 @@
TEST_DEPS = GERRIT_PLUGIN_API + PROVIDED_DEPS + DEPS + [
':sync-events__plugin',
+ ':mockito',
]
gerrit_plugin(
@@ -58,3 +59,30 @@
license = 'Apache2.0',
attach_source = False,
)
+
+maven_jar(
+ name = 'mockito',
+ id = 'org.mockito:mockito-core:2.5.0',
+ sha1 = 'be28d46a52c7f2563580adeca350145e9ce916f8',
+ license = 'MIT',
+ deps = [
+ ':byte-buddy',
+ ':objenesis',
+ ],
+)
+
+maven_jar(
+ name = 'byte-buddy',
+ id = 'net.bytebuddy:byte-buddy:1.5.12',
+ sha1 = 'b1ba1d15f102b36ed43b826488114678d6d413da',
+ license = 'DO_NOT_DISTRIBUTE',
+ attach_source = False,
+)
+
+maven_jar(
+ name = 'objenesis',
+ id = 'org.objenesis:objenesis:2.4',
+ sha1 = '2916b6c96b50c5b3ec4452ed99401db745aabb27',
+ license = 'DO_NOT_DISTRIBUTE',
+ attach_source = False,
+)
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/ConfigurationTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/ConfigurationTest.java
index da0708e..f14df56 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/ConfigurationTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/ConfigurationTest.java
@@ -15,42 +15,46 @@
package com.ericsson.gerrit.plugins.syncevents;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
+import static org.mockito.Mockito.when;
-import org.easymock.EasyMockSupport;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.ericsson.gerrit.plugins.syncevents.Configuration;
import com.google.gerrit.server.config.PluginConfig;
import com.google.gerrit.server.config.PluginConfigFactory;
-public class ConfigurationTest extends EasyMockSupport {
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ConfigurationTest {
private static final String PASS = "fakePass";
private static final String USER = "fakeUser";
private static final String URL = "fakeUrl";
private static final String EMPTY = "";
+ private static final boolean CUSTOM_VALUES = true;
+ private static final boolean DEFAULT_VALUES = false;
private static final int TIMEOUT = 5000;
private static final int MAX_TRIES = 5;
private static final int RETRY_INTERVAL = 1000;
private static final int THREAD_POOL_SIZE = 1;
+ @Mock
private PluginConfigFactory cfgFactoryMock;
+ @Mock
private PluginConfig configMock;
private Configuration configuration;
private String pluginName = "sync-events";
@Before
public void setUp() throws Exception {
- configMock = createNiceMock(PluginConfig.class);
- cfgFactoryMock = createMock(PluginConfigFactory.class);
- expect(cfgFactoryMock.getFromGerritConfig(pluginName, true))
- .andStubReturn(configMock);
+ when(cfgFactoryMock.getFromGerritConfig(pluginName, true))
+ .thenReturn(configMock);
}
@Test
public void testValuesPresentInGerritConfig() throws Exception {
- buildMocks(true);
+ buildMocks(CUSTOM_VALUES);
assertThat(configuration.getUrl()).isEqualTo(URL);
assertThat(configuration.getUser()).isEqualTo(USER);
assertThat(configuration.getPassword()).isEqualTo(PASS);
@@ -63,7 +67,7 @@
@Test
public void testValuesNotPresentInGerritConfig() throws Exception {
- buildMocks(false);
+ buildMocks(DEFAULT_VALUES);
assertThat(configuration.getUrl()).isEqualTo(EMPTY);
assertThat(configuration.getUser()).isEqualTo(EMPTY);
assertThat(configuration.getPassword()).isEqualTo(EMPTY);
@@ -76,28 +80,28 @@
@Test
public void testUrlTrailingSlashIsDropped() throws Exception {
- expect(configMock.getString("url")).andReturn(URL + "/");
- replayAll();
+ when(configMock.getString("url")).thenReturn(URL + "/");
+
configuration = new Configuration(cfgFactoryMock, pluginName);
assertThat(configuration).isNotNull();
assertThat(configuration.getUrl()).isEqualTo(URL);
}
private void buildMocks(boolean values) {
- expect(configMock.getString("url")).andReturn(values ? URL : null);
- expect(configMock.getString("user")).andReturn(values ? USER : null);
- expect(configMock.getString("password")).andReturn(values ? PASS : null);
- expect(configMock.getInt("connectionTimeout", TIMEOUT))
- .andReturn(values ? TIMEOUT : 0);
- expect(configMock.getInt("socketTimeout", TIMEOUT))
- .andReturn(values ? TIMEOUT : 0);
- expect(configMock.getInt("maxTries", MAX_TRIES))
- .andReturn(values ? MAX_TRIES : 0);
- expect(configMock.getInt("retryInterval", RETRY_INTERVAL))
- .andReturn(values ? RETRY_INTERVAL : 0);
- expect(configMock.getInt("threadPoolSize", THREAD_POOL_SIZE))
- .andReturn(values ? THREAD_POOL_SIZE : 0);
- replayAll();
+ when(configMock.getString("url")).thenReturn(values ? URL : null);
+ when(configMock.getString("user")).thenReturn(values ? USER : null);
+ when(configMock.getString("password")).thenReturn(values ? PASS : null);
+ when(configMock.getInt("connectionTimeout", TIMEOUT))
+ .thenReturn(values ? TIMEOUT : 0);
+ when(configMock.getInt("socketTimeout", TIMEOUT))
+ .thenReturn(values ? TIMEOUT : 0);
+ when(configMock.getInt("maxTries", MAX_TRIES))
+ .thenReturn(values ? MAX_TRIES : 0);
+ when(configMock.getInt("retryInterval", RETRY_INTERVAL))
+ .thenReturn(values ? RETRY_INTERVAL : 0);
+ when(configMock.getInt("threadPoolSize", THREAD_POOL_SIZE))
+ .thenReturn(values ? THREAD_POOL_SIZE : 0);
+
configuration = new Configuration(cfgFactoryMock, pluginName);
}
}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/ContextTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/ContextTest.java
index bb36c4d..c3dd4cd 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/ContextTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/ContextTest.java
@@ -16,12 +16,11 @@
import static com.google.common.truth.Truth.assertThat;
-import org.easymock.EasyMockSupport;
import org.junit.Test;
import com.ericsson.gerrit.plugins.syncevents.Context;
-public class ContextTest extends EasyMockSupport {
+public class ContextTest {
@Test
public void testInitialValueNotNull() throws Exception {
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/EventHandlerTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/EventHandlerTest.java
index aefd111..b84be73 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/EventHandlerTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/EventHandlerTest.java
@@ -15,73 +15,69 @@
package com.ericsson.gerrit.plugins.syncevents;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
import com.google.gerrit.server.events.Event;
import com.google.gerrit.server.events.RefEvent;
-import org.easymock.EasyMockSupport;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
import java.util.concurrent.ScheduledThreadPoolExecutor;
-public class EventHandlerTest extends EasyMockSupport {
+@RunWith(MockitoJUnitRunner.class)
+public class EventHandlerTest {
private static final String PLUGIN_NAME = "sync-event";
private Event event;
private EventHandler eventHandler;
- private RestSession restClient;
- private ScheduledThreadPoolExecutor pool;
+ @Mock
+ private RestSession restSession;
@Test
public void testRightEventAndNotForwarded() throws Exception {
- setUpMocks(true, true);
- resetAll();
- expect(restClient.send(event)).andReturn(true);
- replayAll();
+ setUpMocks(true);
eventHandler.onEvent(event);
- verifyAll();
+ verify(restSession).send(event);
}
@Test
- public void testRightEventBitItIsForwarded() throws Exception {
- setUpMocks(false, true);
+ public void testRightEventIsForwarded() throws Exception {
+ setUpMocks(true);
Context.setForwardedEvent();
eventHandler.onEvent(event);
Context.unsetForwardedEvent();
- verifyAll();
+ verifyZeroInteractions(restSession);
}
@Test
public void testBadEventAndNotForwarded() throws Exception {
- setUpMocks(false, false);
+ setUpMocks(false);
eventHandler.onEvent(event);
- verifyAll();
+ verifyZeroInteractions(restSession);
}
@Test
public void testBadEventAndItIsForwarded() throws Exception {
- setUpMocks(false, false);
+ setUpMocks(false);
Context.setForwardedEvent();
eventHandler.onEvent(event);
Context.unsetForwardedEvent();
- verifyAll();
+ verifyZeroInteractions(restSession);
}
- private void setUpMocks(boolean mockRestClient, boolean rightEvent) {
- pool = new PoolMock(1);
- if (mockRestClient) {
- restClient = createMock(RestSession.class);
- } else {
- restClient = null;
- }
+ private void setUpMocks(boolean rightEvent) {
+ ScheduledThreadPoolExecutor pool = new PoolMock(1);
if (rightEvent) {
- event = createNiceMock(RefEvent.class);
+ event = mock(RefEvent.class);
} else {
- event = createNiceMock(Event.class);
+ event = mock(Event.class);
}
- replayAll();
- eventHandler = new EventHandler(restClient, pool, PLUGIN_NAME);
+ eventHandler = new EventHandler(restSession, pool, PLUGIN_NAME);
}
private class PoolMock extends ScheduledThreadPoolExecutor {
@@ -91,8 +87,8 @@
@Override
public void execute(Runnable command) {
- assertThat(command.toString()).isEqualTo(String
- .format("[%s] Send event '%s' to target instance", PLUGIN_NAME, null));
+ assertThat(command.toString()).isEqualTo(String.format(
+ "[%s] Send event '%s' to target instance", PLUGIN_NAME, null));
command.run();
}
}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpClientProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpClientProviderTest.java
index 0af08b4..30b4cf2 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpClientProviderTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpClientProviderTest.java
@@ -15,7 +15,7 @@
package com.ericsson.gerrit.plugins.syncevents;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
+import static org.mockito.Mockito.when;
import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.inject.Guice;
@@ -23,27 +23,27 @@
import com.google.inject.Scopes;
import org.apache.http.impl.client.CloseableHttpClient;
-import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
-public class HttpClientProviderTest extends EasyMockSupport {
+@RunWith(MockitoJUnitRunner.class)
+public class HttpClientProviderTest {
private static final int TIME_INTERVAL = 1000;
private static final String EMPTY = "";
+ @Mock
private Configuration config;
@Before
public void setUp() throws Exception {
- config = createNiceMock(Configuration.class);
- expect(config.getUrl()).andReturn(EMPTY);
- expect(config.getUser()).andReturn(EMPTY);
- expect(config.getPassword()).andReturn(EMPTY);
- expect(config.getMaxTries()).andReturn(1);
- expect(config.getConnectionTimeout()).andReturn(TIME_INTERVAL);
- expect(config.getSocketTimeout()).andReturn(TIME_INTERVAL);
- expect(config.getRetryInterval()).andReturn(TIME_INTERVAL);
- replayAll();
+ when(config.getUrl()).thenReturn(EMPTY);
+ when(config.getUser()).thenReturn(EMPTY);
+ when(config.getPassword()).thenReturn(EMPTY);
+ when(config.getConnectionTimeout()).thenReturn(TIME_INTERVAL);
+ when(config.getSocketTimeout()).thenReturn(TIME_INTERVAL);
}
@Test
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpSessionTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpSessionTest.java
index e7575a2..3b8e182 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpSessionTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/HttpSessionTest.java
@@ -19,26 +19,22 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.easymock.EasyMockSupport;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-
-import com.ericsson.gerrit.plugins.syncevents.Configuration;
-import com.ericsson.gerrit.plugins.syncevents.HttpClientProvider;
-import com.ericsson.gerrit.plugins.syncevents.HttpSession;
import com.ericsson.gerrit.plugins.syncevents.SyncEventsResponseHandler.SyncResult;
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.stubbing.Scenario;
-public class HttpSessionTest extends EasyMockSupport {
- private static final int MAX_TRIES = 5;
- private static final int RETRY_INTERVAL = 1000;
- private static final int TIMEOUT = 1000;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class HttpSessionTest {
+ private static final int MAX_TRIES = 3;
+ private static final int RETRY_INTERVAL = 250;
+ private static final int TIMEOUT = 500;
private static final int ERROR = 500;
private static final int OK = 204;
private static final int NOT_FOUND = 404;
@@ -50,28 +46,26 @@
private static final String REQUEST_MADE = "Request made";
private static final String RETRY_AT_ERROR = "Retry at error";
private static final String RETRY_AT_DELAY = "Retry at delay";
- private static final String URL = "http://localhost:18888";
- private Configuration cfg;
- private CloseableHttpClient httpClient;
private HttpSession httpSession;
@Rule
- public WireMockRule wireMockRule = new WireMockRule(18888);
+ public WireMockRule wireMockRule = new WireMockRule(0);
@Before
public void setUp() throws Exception {
- cfg = createMock(Configuration.class);
- expect(cfg.getUrl()).andReturn(URL).anyTimes();
- expect(cfg.getUser()).andReturn("user");
- expect(cfg.getPassword()).andReturn("pass");
- expect(cfg.getMaxTries()).andReturn(MAX_TRIES).anyTimes();
- expect(cfg.getConnectionTimeout()).andReturn(TIMEOUT).anyTimes();
- expect(cfg.getSocketTimeout()).andReturn(TIMEOUT).anyTimes();
- expect(cfg.getRetryInterval()).andReturn(RETRY_INTERVAL).anyTimes();
- replayAll();
- httpClient = new HttpClientProvider(cfg).get();
- httpSession = new HttpSession(httpClient, URL);
+ String url = "http://localhost:" + wireMockRule.port();
+ Configuration cfg = mock(Configuration.class);
+ when(cfg.getUrl()).thenReturn(url);
+ when(cfg.getUser()).thenReturn("user");
+ when(cfg.getPassword()).thenReturn("pass");
+ when(cfg.getMaxTries()).thenReturn(MAX_TRIES);
+ when(cfg.getConnectionTimeout()).thenReturn(TIMEOUT);
+ when(cfg.getSocketTimeout()).thenReturn(TIMEOUT);
+ when(cfg.getRetryInterval()).thenReturn(RETRY_INTERVAL);
+
+ httpSession =
+ new HttpSession(new HttpClientProvider(cfg).get(), url);
}
@Test
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/ModuleTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/ModuleTest.java
index 5ecab72..3a0addf 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/ModuleTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/ModuleTest.java
@@ -15,22 +15,19 @@
package com.ericsson.gerrit.plugins.syncevents;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
-import org.easymock.EasyMockSupport;
import org.junit.Test;
-import com.ericsson.gerrit.plugins.syncevents.Configuration;
-import com.ericsson.gerrit.plugins.syncevents.Module;
-
-public class ModuleTest extends EasyMockSupport {
+public class ModuleTest {
@Test
public void testSyncUrlProvider() {
- Configuration configMock = createNiceMock(Configuration.class);
+ Configuration configMock = mock(Configuration.class);
String expected = "someUrl";
- expect(configMock.getUrl()).andReturn(expected);
- replayAll();
+ when(configMock.getUrl()).thenReturn(expected);
+
Module module = new Module();
assertThat(module.syncUrl(configMock)).isEqualTo(expected);
}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/RestSessionTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/RestSessionTest.java
index 631e094..9385c5f 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/RestSessionTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/RestSessionTest.java
@@ -15,60 +15,63 @@
package com.ericsson.gerrit.plugins.syncevents;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import com.google.common.base.Joiner;
import com.google.gerrit.server.events.Event;
import com.google.gson.GsonBuilder;
-import com.ericsson.gerrit.plugins.syncevents.HttpSession;
-import com.ericsson.gerrit.plugins.syncevents.RestSession;
import com.ericsson.gerrit.plugins.syncevents.SyncEventsResponseHandler.SyncResult;
-import org.easymock.EasyMockSupport;
+import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
-public class RestSessionTest extends EasyMockSupport {
+public class RestSessionTest {
private static final String PLUGIN_NAME = "sync-events";
+ private static final String REQUEST =
+ Joiner.on("/").join("/plugins", PLUGIN_NAME, "event");
- private RestSession restClient;
+ private RestSession restSession;
private Event event;
+ @Before
+ public void setup() {
+ event = new SyncEventTest();
+ }
+
@Test
public void testEventSentOK() throws Exception {
- event = setUpMocks(true, "", false);
- assertThat(restClient.send(event)).isTrue();
+ setUpMocks(true, "", false);
+ assertThat(restSession.send(event)).isTrue();
}
@Test
public void testEventSentFailed() throws Exception {
- event = setUpMocks(false, "Error", false);
- assertThat(restClient.send(event)).isFalse();
+ setUpMocks(false, "Error", false);
+ assertThat(restSession.send(event)).isFalse();
}
@Test
public void testEventSentThrowsException() throws Exception {
- event = setUpMocks(false, "Exception", true);
- assertThat(restClient.send(event)).isFalse();
+ setUpMocks(false, "Exception", true);
+ assertThat(restSession.send(event)).isFalse();
}
- private Event setUpMocks(boolean ok, String msg, boolean exception)
+ private void setUpMocks(boolean isOk, String msg, boolean throwException)
throws Exception {
- String request = Joiner.on("/").join("/plugins", PLUGIN_NAME, "event");
- SyncEventTest testEvent = new SyncEventTest();
- String content = new GsonBuilder().create().toJson(testEvent);
- HttpSession httpSession = createNiceMock(HttpSession.class);
- if (exception) {
- expect(httpSession.post(request, content)).andThrow(new IOException());
+ String content = new GsonBuilder().create().toJson(event);
+ HttpSession httpSession = mock(HttpSession.class);
+ if (throwException) {
+ doThrow(new IOException()).when(httpSession).post(REQUEST, content);
} else {
- SyncResult result = new SyncResult(ok, msg);
- expect(httpSession.post(request, content)).andReturn(result);
+ SyncResult result = new SyncResult(isOk, msg);
+ when(httpSession.post(REQUEST, content)).thenReturn(result);
}
- restClient = new RestSession(httpSession, PLUGIN_NAME);
- replayAll();
- return testEvent;
+ restSession = new RestSession(httpSession, PLUGIN_NAME);
}
class SyncEventTest extends Event {
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventBrokerTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventBrokerTest.java
index fa304e8..1ca7170 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventBrokerTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventBrokerTest.java
@@ -14,15 +14,18 @@
package com.ericsson.gerrit.plugins.syncevents;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
import com.google.gerrit.common.EventListener;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.server.events.Event;
-import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;
-public class SyncEventBrokerTest extends EasyMockSupport {
+public class SyncEventBrokerTest {
private EventListener listenerMock;
private SyncEventBroker broker;
@@ -30,7 +33,7 @@
@Before
public void setUp() {
- listenerMock = createMock(EventListener.class);
+ listenerMock = mock(EventListener.class);
DynamicSet<EventListener> listeners = DynamicSet.emptySet();
listeners.add(listenerMock);
broker = new SyncEventBroker(null, listeners, null, null, null);
@@ -38,21 +41,18 @@
@Test
public void shouldDispatchEvent() {
- listenerMock.onEvent(event);
- replayAll();
broker.fireEventForUnrestrictedListeners(event);
- verifyAll();
+ verify(listenerMock).onEvent(event);
}
@Test
public void shouldNotDispatchForwardedEvents() {
- replayAll();
Context.setForwardedEvent();
try {
broker.fireEventForUnrestrictedListeners(event);
} finally {
Context.unsetForwardedEvent();
}
- verifyAll();
+ verifyZeroInteractions(listenerMock);
}
}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventExecutorProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventExecutorProviderTest.java
index 006d32b..676e028 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventExecutorProviderTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventExecutorProviderTest.java
@@ -15,29 +15,32 @@
package com.ericsson.gerrit.plugins.syncevents;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import com.google.gerrit.server.git.WorkQueue;
-import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
-public class SyncEventExecutorProviderTest extends EasyMockSupport {
+@RunWith(MockitoJUnitRunner.class)
+public class SyncEventExecutorProviderTest {
+ @Mock
private WorkQueue.Executor executorMock;
private SyncEventExecutorProvider syncEventsExecutorProvider;
@Before
public void setUp() throws Exception {
- executorMock = createStrictMock(WorkQueue.Executor.class);
- WorkQueue workQueueMock = createNiceMock(WorkQueue.class);
- expect(
- workQueueMock.createQueue(4, "Sync stream events [SyncEvents plugin]"))
- .andReturn(executorMock);
- Configuration configMock = createStrictMock(Configuration.class);
- expect(configMock.getThreadPoolSize()).andReturn(4);
- replayAll();
+ WorkQueue workQueueMock = mock(WorkQueue.class);
+ when(workQueueMock.createQueue(4, "Sync stream events [SyncEvents plugin]"))
+ .thenReturn(executorMock);
+ Configuration configMock = mock(Configuration.class);
+ when(configMock.getThreadPoolSize()).thenReturn(4);
+
syncEventsExecutorProvider =
new SyncEventExecutorProvider(workQueueMock, "SyncEvents", configMock);
}
@@ -49,18 +52,11 @@
@Test
public void testStop() throws Exception {
- resetAll();
- executorMock.shutdown();
- expectLastCall().once();
- executorMock.unregisterWorkQueue();
- expectLastCall().once();
- replayAll();
-
syncEventsExecutorProvider.start();
assertThat(syncEventsExecutorProvider.get()).isEqualTo(executorMock);
syncEventsExecutorProvider.stop();
- verifyAll();
+ verify(executorMock).shutdown();
+ verify(executorMock).unregisterWorkQueue();
assertThat(syncEventsExecutorProvider.get()).isNull();
}
}
-
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsResponseHandlerTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsResponseHandlerTest.java
index 0817a29..e17e283 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsResponseHandlerTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsResponseHandlerTest.java
@@ -15,21 +15,20 @@
package com.ericsson.gerrit.plugins.syncevents;
import static com.google.common.truth.Truth.assertThat;
-import static org.easymock.EasyMock.expect;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
-import com.ericsson.gerrit.plugins.syncevents.SyncEventsResponseHandler;
import com.ericsson.gerrit.plugins.syncevents.SyncEventsResponseHandler.SyncResult;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.entity.StringEntity;
-import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
-public class SyncEventsResponseHandlerTest extends EasyMockSupport {
+public class SyncEventsResponseHandlerTest {
private static final int ERROR = 400;
private static final int OK = 204;
private static final String EMPTY_ENTITY = "";
@@ -60,12 +59,11 @@
private HttpResponse setupMocks(int httpCode, String entity)
throws UnsupportedEncodingException {
- StatusLine status = createNiceMock(StatusLine.class);
- expect(status.getStatusCode()).andReturn(httpCode).anyTimes();
- HttpResponse response = createNiceMock(HttpResponse.class);
- expect(response.getStatusLine()).andReturn(status).anyTimes();
- expect(response.getEntity()).andReturn(new StringEntity(entity)).anyTimes();
- replayAll();
+ StatusLine status = mock(StatusLine.class);
+ when(status.getStatusCode()).thenReturn(httpCode);
+ HttpResponse response = mock(HttpResponse.class);
+ when(response.getStatusLine()).thenReturn(status);
+ when(response.getEntity()).thenReturn(new StringEntity(entity));
return response;
}
}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java
index e925e6f..5c318c8 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java
@@ -19,9 +19,10 @@
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.expectLastCall;
-import static org.easymock.EasyMock.isA;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import com.google.common.net.MediaType;
import com.google.gerrit.common.EventDispatcher;
@@ -32,10 +33,12 @@
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.StandardKeyEncoder;
-import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
import java.io.BufferedReader;
import java.io.IOException;
@@ -44,25 +47,29 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-public class SyncEventsRestApiServletTest extends EasyMockSupport {
+@RunWith(MockitoJUnitRunner.class)
+public class SyncEventsRestApiServletTest {
+ private static final String ERR_MSG = "some Error";
- private EventDispatcher dispatcher = createStrictMock(EventDispatcher.class);
- private SyncEventsRestApiServlet syncEventsRestApiServlet;
+ @Mock
+ private EventDispatcher dispatcher;
+ @Mock
private HttpServletRequest req;
+ @Mock
private HttpServletResponse rsp;
+ private SyncEventsRestApiServlet syncEventsRestApiServlet;
@BeforeClass
public static void setup() {
- EventTypes.register(RefReplicationDoneEvent.TYPE, RefReplicationDoneEvent.class);
+ EventTypes.register(RefReplicationDoneEvent.TYPE,
+ RefReplicationDoneEvent.class);
KeyUtil.setEncoderImpl(new StandardKeyEncoder());
}
@Before
public void createSyncEventsRestApiServlet() throws Exception {
syncEventsRestApiServlet = new SyncEventsRestApiServlet(dispatcher);
- req = createNiceMock(HttpServletRequest.class);
- rsp = createNiceMock(HttpServletResponse.class);
- expect(req.getContentType()).andReturn(MediaType.JSON_UTF_8.toString());
+ when(req.getContentType()).thenReturn(MediaType.JSON_UTF_8.toString());
}
@Test
@@ -70,15 +77,11 @@
String event = "{\"project\":\"gerrit/test-sync-index\",\"ref\":"
+ "\"refs/changes/76/669676/2\",\"nodesCount\":1,\"type\":"
+ "\"ref-replication-done\",\"eventCreatedOn\":1451415011}";
- expect(req.getReader())
- .andReturn(new BufferedReader(new StringReader(event)));
- dispatcher.postEvent(isA(RefReplicationDoneEvent.class));
- rsp.setStatus(SC_NO_CONTENT);
- expectLastCall().once();
- replayAll();
-
+ when(req.getReader())
+ .thenReturn(new BufferedReader(new StringReader(event)));
+ dispatcher.postEvent(any(RefReplicationDoneEvent.class));
syncEventsRestApiServlet.doPost(req, rsp);
- verifyAll();
+ verify(rsp).setStatus(SC_NO_CONTENT);
}
@Test
@@ -86,47 +89,36 @@
String event = "{\"project\":\"gerrit/test-sync-index\",\"ref\":"
+ "\"refs/changes/76/669676/2\",\"nodesCount\":1,\"type\":"
+ "\"ref-replication-done\",\"eventCreatedOn\":1451415011}";
- expect(req.getReader())
- .andReturn(new BufferedReader(new StringReader(event)));
- dispatcher.postEvent(isA(RefReplicationDoneEvent.class));
- expectLastCall().andThrow(new OrmException("some Error"));
- rsp.sendError(SC_NOT_FOUND, "Change not found\n");
- expectLastCall().once();
- replayAll();
-
+ when(req.getReader())
+ .thenReturn(new BufferedReader(new StringReader(event)));
+ doThrow(new OrmException(ERR_MSG)).when(dispatcher)
+ .postEvent(any(RefReplicationDoneEvent.class));
syncEventsRestApiServlet.doPost(req, rsp);
- verifyAll();
+ verify(rsp).sendError(SC_NOT_FOUND, "Change not found\n");
}
@Test
public void testDoPostBadRequest() throws Exception {
- expect(req.getReader()).andThrow(new IOException());
- replayAll();
+ doThrow(new IOException(ERR_MSG)).when(req).getReader();
syncEventsRestApiServlet.doPost(req, rsp);
- verifyAll();
+ verify(rsp).sendError(SC_BAD_REQUEST, ERR_MSG);
}
@Test
public void testDoPostWrongMediaType() throws Exception {
- resetAll();
- expect(req.getContentType())
- .andReturn(MediaType.APPLICATION_XML_UTF_8.toString()).anyTimes();
- rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE,
- "Expecting " + JSON_UTF_8.toString() + " content type");
- expectLastCall().once();
- replayAll();
+ when(req.getContentType())
+ .thenReturn(MediaType.APPLICATION_XML_UTF_8.toString());
syncEventsRestApiServlet.doPost(req, rsp);
- verifyAll();
+ verify(rsp).sendError(SC_UNSUPPORTED_MEDIA_TYPE,
+ "Expecting " + JSON_UTF_8.toString() + " content type");
}
@Test
public void testDoPostErrorWhileSendingErrorMessage() throws Exception {
- expect(req.getReader()).andThrow(new IOException("someError"));
- rsp.sendError(SC_BAD_REQUEST, "someError");
- expectLastCall().andThrow(new IOException("someOtherError"));
- replayAll();
+ doThrow(new IOException(ERR_MSG)).when(req).getReader();
+ doThrow(new IOException("someOtherError")).when(rsp)
+ .sendError(SC_BAD_REQUEST, ERR_MSG);
syncEventsRestApiServlet.doPost(req, rsp);
- verifyAll();
}
static class RefReplicationDoneEvent extends RefEvent {