Use assertThrows instead of ExpectedException
This change is based on one generated by an automated refactoring tool.
See [1] for context.
[1] https://gerrit-review.googlesource.com/c/gerrit/+/223155
Change-Id: Ia6cc3a263355c985557e5d876e7d8dd2b77a39fa
diff --git a/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java b/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
index 4920df3..ea2784d 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/automerger/ConfigLoaderIT.java
@@ -15,6 +15,7 @@
package com.googlesource.gerrit.plugins.automerger;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
@@ -185,9 +186,13 @@
public void downstreamBranchesTest_configException() throws Exception {
defaultSetup("wrong.config");
- exception.expect(ConfigInvalidException.class);
- exception.expectMessage("Automerger config branch pair malformed: master..ds_one");
- configLoader.getDownstreamBranches("master", "platform/some/project");
+ ConfigInvalidException thrown =
+ assertThrows(
+ ConfigInvalidException.class,
+ () -> configLoader.getDownstreamBranches("master", "platform/some/project"));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains("Automerger config branch pair malformed: master..ds_one");
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/automerger/DownstreamCreatorIT.java b/src/test/java/com/googlesource/gerrit/plugins/automerger/DownstreamCreatorIT.java
index d04b4ec..70008c3 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/automerger/DownstreamCreatorIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/automerger/DownstreamCreatorIT.java
@@ -24,6 +24,7 @@
import static com.google.gerrit.extensions.client.ListChangesOption.CURRENT_REVISION;
import static com.google.gerrit.extensions.client.ListChangesOption.DETAILED_LABELS;
import static com.google.gerrit.extensions.client.ListChangesOption.MESSAGES;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static java.util.Comparator.comparing;
import com.google.common.base.Charsets;
@@ -846,9 +847,8 @@
assertCodeReview(dsOneChangeInfo.id, 2, "autogenerated:Automerger");
// Try to +2 downstream and see it fail
- exception.expect(AuthException.class);
- exception.expectMessage("Applying label \"Code-Review\": 2 is restricted");
- approve(dsOneChangeInfo.id);
+ AuthException thrown = assertThrows(AuthException.class, () -> approve(dsOneChangeInfo.id));
+ assertThat(thrown).hasMessageThat().contains("Applying label \"Code-Review\": 2 is restricted");
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/automerger/MergeValidatorIT.java b/src/test/java/com/googlesource/gerrit/plugins/automerger/MergeValidatorIT.java
index c1e0f63..2969d30 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/automerger/MergeValidatorIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/automerger/MergeValidatorIT.java
@@ -15,6 +15,7 @@
package com.googlesource.gerrit.plugins.automerger;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
@@ -100,12 +101,14 @@
gApi.changes().id(sortedChanges.get(0)._number).abandon();
int changeNumber = result.getChange().getId().get();
- exception.expect(ResourceConflictException.class);
- exception.expectMessage(
- "Failed to submit 1 change due to the following problems:\nChange "
- + changeNumber
- + ": Missing downstream branches ds_one. Please recreate the automerges.");
- merge(result);
+ ResourceConflictException thrown =
+ assertThrows(ResourceConflictException.class, () -> merge(result));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains(
+ "Failed to submit 1 change due to the following problems:\nChange "
+ + changeNumber
+ + ": Missing downstream branches ds_one. Please recreate the automerges.");
}
@Test
@@ -164,12 +167,14 @@
result.assertOkStatus();
int changeNumber = result.getChange().getId().get();
// Assert we are missing downstreams
- exception.expect(ResourceConflictException.class);
- exception.expectMessage(
- "Failed to submit 1 change due to the following problems:\nChange "
- + changeNumber
- + ": Missing downstream branches ds_one. Please recreate the automerges.");
- merge(result);
+ ResourceConflictException thrown =
+ assertThrows(ResourceConflictException.class, () -> merge(result));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains(
+ "Failed to submit 1 change due to the following problems:\nChange "
+ + changeNumber
+ + ": Missing downstream branches ds_one. Please recreate the automerges.");
}
@Test
@@ -181,12 +186,14 @@
result.assertOkStatus();
int changeNumber = result.getChange().getId().get();
// Assert we are missing downstreams
- exception.expect(ResourceConflictException.class);
- exception.expectMessage(
- "Failed to submit 1 change due to the following problems:\nChange "
- + changeNumber
- + ": there is no ds_one");
- merge(result);
+ ResourceConflictException thrown =
+ assertThrows(ResourceConflictException.class, () -> merge(result));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains(
+ "Failed to submit 1 change due to the following problems:\nChange "
+ + changeNumber
+ + ": there is no ds_one");
}
private List<ChangeInfo> sortedChanges(List<ChangeInfo> changes) {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/automerger/QueryBuilderTest.java b/src/test/java/com/googlesource/gerrit/plugins/automerger/QueryBuilderTest.java
index fd46cb8..f16c986 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/automerger/QueryBuilderTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/automerger/QueryBuilderTest.java
@@ -15,17 +15,15 @@
package com.googlesource.gerrit.plugins.automerger;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class QueryBuilderTest {
- @Rule public ExpectedException exception = ExpectedException.none();
private QueryBuilder queryBuilder;
@Before
@@ -42,9 +40,12 @@
@Test
public void nullTest() throws Exception {
queryBuilder.addParameter("status", "open");
- exception.expect(InvalidQueryParameterException.class);
- exception.expectMessage("Cannot use null value for key or value of query.");
- queryBuilder.addParameter("topic", null);
+ InvalidQueryParameterException thrown =
+ assertThrows(
+ InvalidQueryParameterException.class, () -> queryBuilder.addParameter("topic", null));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains("Cannot use null value for key or value of query.");
}
@Test
@@ -75,8 +76,12 @@
@Test
public void errorOnQuotesAndBracesTest() throws Exception {
- exception.expect(InvalidQueryParameterException.class);
- exception.expectMessage("Gerrit does not support both quotes and braces in a query.");
- queryBuilder.addParameter("topic", "topic{\"with\"}both");
+ InvalidQueryParameterException thrown =
+ assertThrows(
+ InvalidQueryParameterException.class,
+ () -> queryBuilder.addParameter("topic", "topic{\"with\"}both"));
+ assertThat(thrown)
+ .hasMessageThat()
+ .contains("Gerrit does not support both quotes and braces in a query.");
}
}