Migrate from try-catch-fail to assertThrows
Change-Id: Ie67a82b970bfcc76cc48bc9a96feb90d748db776
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedEventHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedEventHandlerTest.java
index 5c36ada..387fc41 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedEventHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedEventHandlerTest.java
@@ -15,7 +15,7 @@
package com.googlesource.gerrit.plugins.multisite.forwarder;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
@@ -87,12 +87,8 @@
.postEvent(event);
assertThat(Context.isForwardedEvent()).isFalse();
- try {
- handler.dispatch(event);
- fail("should have throw an StorageException");
- } catch (StorageException e) {
- assertThat(e.getMessage()).isEqualTo("someMessage");
- }
+ StorageException thrown = assertThrows(StorageException.class, () -> handler.dispatch(event));
+ assertThat(thrown).hasMessageThat().isEqualTo("someMessage");
assertThat(Context.isForwardedEvent()).isFalse();
verify(dispatcherMock).postEvent(event);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandlerTest.java
index e67ba0f..83c79c1 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexAccountHandlerTest.java
@@ -15,7 +15,7 @@
package com.googlesource.gerrit.plugins.multisite.forwarder;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -98,12 +98,9 @@
.index(id);
assertThat(Context.isForwardedEvent()).isFalse();
- try {
- handler.index(id, Operation.INDEX, Optional.empty());
- fail("should have thrown an IOException");
- } catch (IOException e) {
- assertThat(e.getMessage()).isEqualTo("someMessage");
- }
+ IOException thrown =
+ assertThrows(IOException.class, () -> handler.index(id, Operation.INDEX, Optional.empty()));
+ assertThat(thrown).hasMessageThat().isEqualTo("someMessage");
assertThat(Context.isForwardedEvent()).isFalse();
verify(indexerMock).index(id);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java
index 95271df..6255d32 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java
@@ -15,7 +15,7 @@
package com.googlesource.gerrit.plugins.multisite.forwarder;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
@@ -163,12 +163,11 @@
.index(any(Change.class));
assertThat(Context.isForwardedEvent()).isFalse();
- try {
- handler.index(TEST_CHANGE_ID, Operation.INDEX, Optional.empty());
- fail("should have thrown an IOException");
- } catch (IOException e) {
- assertThat(e.getMessage()).isEqualTo("someMessage");
- }
+ IOException thrown =
+ assertThrows(
+ IOException.class,
+ () -> handler.index(TEST_CHANGE_ID, Operation.INDEX, Optional.empty()));
+ assertThat(thrown).hasMessageThat().isEqualTo("someMessage");
assertThat(Context.isForwardedEvent()).isFalse();
verify(indexerMock, times(1)).index(any(Change.class));
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexGroupHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexGroupHandlerTest.java
index dfb04c9..3b248aa 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexGroupHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexGroupHandlerTest.java
@@ -15,7 +15,7 @@
package com.googlesource.gerrit.plugins.multisite.forwarder;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -98,12 +98,10 @@
.index(AccountGroup.uuid(uuid));
assertThat(Context.isForwardedEvent()).isFalse();
- try {
- handler.index(uuid, Operation.INDEX, Optional.empty());
- fail("should have thrown an IOException");
- } catch (IOException e) {
- assertThat(e.getMessage()).isEqualTo("someMessage");
- }
+ IOException thrown =
+ assertThrows(
+ IOException.class, () -> handler.index(uuid, Operation.INDEX, Optional.empty()));
+ assertThat(thrown).hasMessageThat().isEqualTo("someMessage");
assertThat(Context.isForwardedEvent()).isFalse();
verify(indexerMock).index(AccountGroup.uuid(uuid));
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexProjectHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexProjectHandlerTest.java
index 9e0ce51..0324ae2 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexProjectHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexProjectHandlerTest.java
@@ -15,7 +15,7 @@
package com.googlesource.gerrit.plugins.multisite.forwarder;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
@@ -108,12 +108,10 @@
.index(Project.nameKey(nameKey));
assertThat(Context.isForwardedEvent()).isFalse();
- try {
- handler.index(nameKey, Operation.INDEX, Optional.empty());
- fail("should have thrown an IOException");
- } catch (IOException e) {
- assertThat(e.getMessage()).isEqualTo("someMessage");
- }
+ IOException thrown =
+ assertThrows(
+ IOException.class, () -> handler.index(nameKey, Operation.INDEX, Optional.empty()));
+ assertThat(thrown).hasMessageThat().isEqualTo("someMessage");
assertThat(Context.isForwardedEvent()).isFalse();
verify(indexerMock).index(Project.nameKey(nameKey));
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedProjectListUpdateHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedProjectListUpdateHandlerTest.java
index 8422ecf..a911ef1 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedProjectListUpdateHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedProjectListUpdateHandlerTest.java
@@ -15,7 +15,7 @@
package com.googlesource.gerrit.plugins.multisite.forwarder;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
@@ -110,12 +110,11 @@
.onCreateProject(PROJECT_KEY);
assertThat(Context.isForwardedEvent()).isFalse();
- try {
- handler.update(new ProjectListUpdateEvent(PROJECT_NAME, false));
- fail("should have thrown a RuntimeException");
- } catch (RuntimeException e) {
- assertThat(e.getMessage()).isEqualTo(SOME_MESSAGE);
- }
+ RuntimeException thrown =
+ assertThrows(
+ RuntimeException.class,
+ () -> handler.update(new ProjectListUpdateEvent(PROJECT_NAME, false)));
+ assertThat(thrown).hasMessageThat().isEqualTo(SOME_MESSAGE);
assertThat(Context.isForwardedEvent()).isFalse();
verify(projectCacheMock).onCreateProject(PROJECT_KEY);
@@ -133,12 +132,11 @@
.remove(PROJECT_KEY);
assertThat(Context.isForwardedEvent()).isFalse();
- try {
- handler.update(new ProjectListUpdateEvent(PROJECT_NAME, true));
- fail("should have thrown a RuntimeException");
- } catch (RuntimeException e) {
- assertThat(e.getMessage()).isEqualTo(SOME_MESSAGE);
- }
+ RuntimeException thrown =
+ assertThrows(
+ RuntimeException.class,
+ () -> handler.update(new ProjectListUpdateEvent(PROJECT_NAME, true)));
+ assertThat(thrown).hasMessageThat().isEqualTo(SOME_MESSAGE);
assertThat(Context.isForwardedEvent()).isFalse();
verify(projectCacheMock).remove(PROJECT_KEY);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/kafka/consumer/IndexEventRouterTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/kafka/consumer/IndexEventRouterTest.java
index 82a8d70..750f466 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/kafka/consumer/IndexEventRouterTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/kafka/consumer/IndexEventRouterTest.java
@@ -14,6 +14,7 @@
package com.googlesource.gerrit.plugins.multisite.kafka.consumer;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -30,7 +31,6 @@
import com.googlesource.gerrit.plugins.multisite.forwarder.events.ProjectIndexEvent;
import com.googlesource.gerrit.plugins.multisite.kafka.router.IndexEventRouter;
import java.util.Optional;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -123,12 +123,8 @@
public void routerShouldFailForNotRecognisedEvents() throws Exception {
final IndexEvent newEventType = new IndexEvent("new-type") {};
- try {
- router.route(newEventType);
- Assert.fail("Expected exception for not supported event");
- } catch (UnsupportedOperationException expected) {
- verifyZeroInteractions(
- indexAccountHandler, indexChangeHandler, indexGroupHandler, indexProjectHandler);
- }
+ assertThrows(UnsupportedOperationException.class, () -> router.route(newEventType));
+ verifyZeroInteractions(
+ indexAccountHandler, indexChangeHandler, indexGroupHandler, indexProjectHandler);
}
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteBatchRefUpdateTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteBatchRefUpdateTest.java
index 57fa210..45600d0 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteBatchRefUpdateTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteBatchRefUpdateTest.java
@@ -14,8 +14,8 @@
package com.googlesource.gerrit.plugins.multisite.validation;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static java.util.Arrays.asList;
-import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
@@ -128,12 +128,10 @@
setMockRequiredReturnValues();
doReturn(false).when(sharedRefDb).isUpToDate(A_TEST_PROJECT_NAME, oldRef);
- try {
- multiSiteRefUpdate.execute(revWalk, progressMonitor, Collections.emptyList());
- fail("Expecting an IOException to be thrown");
- } catch (IOException e) {
- verify(validationMetrics).incrementSplitBrainPrevention();
- }
+ assertThrows(
+ IOException.class,
+ () -> multiSiteRefUpdate.execute(revWalk, progressMonitor, Collections.emptyList()));
+ verify(validationMetrics).incrementSplitBrainPrevention();
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteRefUpdateTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteRefUpdateTest.java
index 8dcaf3a..c20ac92 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteRefUpdateTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/MultiSiteRefUpdateTest.java
@@ -15,7 +15,7 @@
package com.googlesource.gerrit.plugins.multisite.validation;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -101,12 +101,8 @@
MultiSiteRefUpdate multiSiteRefUpdate =
getMultiSiteRefUpdateWithDefaultPolicyEnforcement(refUpdate);
- try {
- multiSiteRefUpdate.update();
- fail("Expecting an IOException to be thrown");
- } catch (IOException e) {
- verify(validationMetrics).incrementSplitBrainPrevention();
- }
+ assertThrows(IOException.class, () -> multiSiteRefUpdate.update());
+ verify(validationMetrics).incrementSplitBrainPrevention();
}
@Test
@@ -123,12 +119,8 @@
MultiSiteRefUpdate multiSiteRefUpdate =
getMultiSiteRefUpdateWithDefaultPolicyEnforcement(refUpdate);
- try {
- multiSiteRefUpdate.update();
- fail("Expecting an IOException to be thrown");
- } catch (IOException e) {
- verify(validationMetrics, never()).incrementSplitBrainPrevention();
- }
+ assertThrows(IOException.class, () -> multiSiteRefUpdate.update());
+ verify(validationMetrics, never()).incrementSplitBrainPrevention();
}
@Test
@@ -145,12 +137,8 @@
MultiSiteRefUpdate multiSiteRefUpdate =
getMultiSiteRefUpdateWithDefaultPolicyEnforcement(refUpdate);
- try {
- multiSiteRefUpdate.update();
- fail("Expecting an IOException to be thrown");
- } catch (IOException e) {
- verify(validationMetrics).incrementSplitBrain();
- }
+ assertThrows(IOException.class, () -> multiSiteRefUpdate.update());
+ verify(validationMetrics).incrementSplitBrain();
}
@Test
@@ -178,12 +166,8 @@
MultiSiteRefUpdate multiSiteRefUpdate =
getMultiSiteRefUpdateWithDefaultPolicyEnforcement(refUpdate);
- try {
- multiSiteRefUpdate.delete();
- fail("Expecting an IOException to be thrown");
- } catch (IOException e) {
- verify(validationMetrics).incrementSplitBrainPrevention();
- }
+ assertThrows(IOException.class, () -> multiSiteRefUpdate.delete());
+ verify(validationMetrics).incrementSplitBrainPrevention();
}
private MultiSiteRefUpdate getMultiSiteRefUpdateWithDefaultPolicyEnforcement(