Stop using deprecated AbstractDaemonTest#fail Migrate to the new GerritJUnit#assertThrows where testing for expected exceptions, and inline Truth's assert_().fail() otherwise. Change-Id: I6a055d4d0eca0e1a1acd47ea6bbcf0aca2c472a8
diff --git a/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java b/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java index aef9f78..f5bfbe2 100644 --- a/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java +++ b/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java
@@ -15,6 +15,8 @@ package com.googlesource.gerrit.plugins.supermanifest; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assert_; +import static com.google.gerrit.testing.GerritJUnit.assertThrows; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.gerrit.acceptance.GitUtil; @@ -128,12 +130,7 @@ BranchApi branch = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch"); assertThat(branch.file("project1").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); - try { - branch.file("project2"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch.file("project2")); xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" @@ -157,8 +154,8 @@ .to("refs/heads/srcbranch") .assertOkStatus(); - branch = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch"); - assertThat(branch.file("project2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); + BranchApi branch2 = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch"); + assertThat(branch2.file("project2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); // Make sure config change gets picked up. pushConfig( @@ -189,8 +186,8 @@ .to("refs/heads/srcbranch") .assertOkStatus(); - branch = gApi.projects().name(superKey.get()).branch("refs/heads/other"); - assertThat(branch.file("project3").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); + BranchApi branch3 = gApi.projects().name(superKey.get()).branch("refs/heads/other"); + assertThat(branch3.file("project3").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); } @Test @@ -356,12 +353,7 @@ assertThat(branch.file("project1").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); assertThat(branch.file("manifest2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); assertThat(branch.file("manifest2").asString()).contains(commit.name()); - try { - branch.file("project2"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch.file("project2")); } @Test @@ -454,12 +446,7 @@ BranchApi branch = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch"); assertThat(branch.file("project1").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); assertThat(branch.file("manifest2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); - try { - branch.file("project2"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch.file("project2")); } private void outer() throws Exception { @@ -471,17 +458,13 @@ } private void innerTest() throws Exception { - try { - outer(); - fail("should throw"); - } catch (IllegalStateException e) { - StackTraceElement[] trimmed = - SuperManifestRefUpdatedListener.trimStack( - e.getStackTrace(), Thread.currentThread().getStackTrace()[1]); - String str = Arrays.toString(trimmed); - assertThat(str).doesNotContain("trimStackTrace"); - assertThat(str).contains("innerTest"); - } + IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> outer()); + StackTraceElement[] trimmed = + SuperManifestRefUpdatedListener.trimStack( + thrown.getStackTrace(), Thread.currentThread().getStackTrace()[1]); + String str = Arrays.toString(trimmed); + assertThat(str).doesNotContain("trimStackTrace"); + assertThat(str).contains("innerTest"); } @Test @@ -546,21 +529,11 @@ BranchApi branch1 = gApi.projects().name(superKey.get()).branch("refs/heads/src1"); assertThat(branch1.file("project1").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); - try { - branch1.file("project2"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch1.file("project2")); BranchApi branch2 = gApi.projects().name(superKey.get()).branch("refs/heads/src2"); assertThat(branch2.file("project2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); - try { - branch2.file("project1"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch2.file("project1")); } @Test @@ -764,12 +737,7 @@ .assertOkStatus(); BranchApi branch = gApi.projects().name(superKey.get()).branch("refs/heads/master"); - try { - branch.file("project1"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch.file("project1")); } /* @@ -779,7 +747,7 @@ String got = JiriUpdater.relativize(URI.create(a), URI.create(b)).toString(); if (!got.equals(want)) { - fail(String.format("relative('%s', '%s') = '%s', want '%s'", a, b, got, want)); + assert_().fail("relative('%s', '%s') = '%s', want '%s'", a, b, got, want); } }
diff --git a/javatests/com/googlesource/gerrit/plugins/supermanifest/RepoSuperManifestIT.java b/javatests/com/googlesource/gerrit/plugins/supermanifest/RepoSuperManifestIT.java index 3f012c6..2db98ec 100644 --- a/javatests/com/googlesource/gerrit/plugins/supermanifest/RepoSuperManifestIT.java +++ b/javatests/com/googlesource/gerrit/plugins/supermanifest/RepoSuperManifestIT.java
@@ -15,6 +15,7 @@ package com.googlesource.gerrit.plugins.supermanifest; import static com.google.common.truth.Truth.assertThat; +import static com.google.gerrit.testing.GerritJUnit.assertThrows; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.gerrit.acceptance.GitUtil; @@ -122,12 +123,7 @@ BranchApi branch = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch"); assertThat(branch.file("project1").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); - try { - branch.file("project2"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch.file("project2")); xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" @@ -147,8 +143,8 @@ .to("refs/heads/srcbranch") .assertOkStatus(); - branch = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch"); - assertThat(branch.file("project2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); + BranchApi branch2 = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch"); + assertThat(branch2.file("project2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); // Make sure config change gets picked up. pushConfig( @@ -177,8 +173,8 @@ .to("refs/heads/srcbranch") .assertOkStatus(); - branch = gApi.projects().name(superKey.get()).branch("refs/heads/other"); - assertThat(branch.file("project3").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); + BranchApi branch3 = gApi.projects().name(superKey.get()).branch("refs/heads/other"); + assertThat(branch3.file("project3").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); } @Test @@ -363,17 +359,13 @@ } private void innerTest() throws Exception { - try { - outer(); - fail("should throw"); - } catch (IllegalStateException e) { - StackTraceElement[] trimmed = - SuperManifestRefUpdatedListener.trimStack( - e.getStackTrace(), Thread.currentThread().getStackTrace()[1]); - String str = Arrays.toString(trimmed); - assertThat(str).doesNotContain("trimStackTrace"); - assertThat(str).contains("innerTest"); - } + IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> outer()); + StackTraceElement[] trimmed = + SuperManifestRefUpdatedListener.trimStack( + thrown.getStackTrace(), Thread.currentThread().getStackTrace()[1]); + String str = Arrays.toString(trimmed); + assertThat(str).doesNotContain("trimStackTrace"); + assertThat(str).contains("innerTest"); } @Test @@ -438,21 +430,11 @@ BranchApi branch1 = gApi.projects().name(superKey.get()).branch("refs/heads/src1"); assertThat(branch1.file("project1").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); - try { - branch1.file("project2"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch1.file("project2")); BranchApi branch2 = gApi.projects().name(superKey.get()).branch("refs/heads/src2"); assertThat(branch2.file("project2").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8"); - try { - branch2.file("project1"); - fail("wanted exception"); - } catch (ResourceNotFoundException e) { - // all fine. - } + assertThrows(ResourceNotFoundException.class, () -> branch2.file("project1")); } @Test