Skip to content

Commit

Permalink
Use ExpectFailure instead of try-catch-fail.
Browse files Browse the repository at this point in the history
This resolves some [`AssertionFailureIgnored`](https://errorprone.info/bugpattern/AssertionFailureIgnored) findings, and it provides the usual [additional testing that comes with `ExpectFailure`](https://truth.dev/ExpectFailure).

PiperOrigin-RevId: 518665538
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Mar 23, 2023
1 parent 92fe462 commit 3ec9800
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,16 @@ public void testContainsAtLeast_inOrder() throws Exception {

@Test
public void testContainsAtLeast_inOrder_fails() throws Exception {
try {
assertThat(IntStream.of(42, 43)).containsAtLeast(43, 42).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting.that(IntStream.of(42, 43)).containsAtLeast(43, 42).inOrder());
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}

@Test
Expand All @@ -237,17 +236,19 @@ public void testContainsAtLeastElementsIn_inOrder() throws Exception {

@Test
public void testContainsAtLeastElementsIn_inOrder_fails() throws Exception {
try {
assertThat(IntStream.of(42, 43)).containsAtLeastElementsIn(asList(43, 42)).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(IntStream.of(42, 43))
.containsAtLeastElementsIn(asList(43, 42))
.inOrder());
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}

@Test
Expand All @@ -257,13 +258,10 @@ public void testContainsExactly() throws Exception {

@Test
public void testContainsExactly_fails() throws Exception {
try {
assertThat(IntStream.of(42, 43)).containsExactly(42);
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}
AssertionError expected =
expectFailure(whenTesting -> whenTesting.that(IntStream.of(42, 43)).containsExactly(42));
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}

@Test
Expand All @@ -273,13 +271,12 @@ public void testContainsExactly_inOrder() throws Exception {

@Test
public void testContainsExactly_inOrder_fails() throws Exception {
try {
assertThat(IntStream.of(42, 43)).containsExactly(43, 42).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting.that(IntStream.of(42, 43)).containsExactly(43, 42).inOrder());
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}

@Test
Expand All @@ -290,13 +287,12 @@ public void testContainsExactlyElementsIn() throws Exception {

@Test
public void testContainsExactlyElementsIn_fails() throws Exception {
try {
assertThat(IntStream.of(42, 43)).containsExactlyElementsIn(asList(42));
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting.that(IntStream.of(42, 43)).containsExactlyElementsIn(asList(42)));
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}

@Test
Expand All @@ -306,13 +302,15 @@ public void testContainsExactlyElementsIn_inOrder() throws Exception {

@Test
public void testContainsExactlyElementsIn_inOrder_fails() throws Exception {
try {
assertThat(IntStream.of(42, 43)).containsExactlyElementsIn(asList(43, 42)).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(IntStream.of(42, 43))
.containsExactlyElementsIn(asList(43, 42))
.inOrder());
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,16 @@ public void testContainsAtLeast_inOrder() throws Exception {

@Test
public void testContainsAtLeast_inOrder_fails() throws Exception {
try {
assertThat(LongStream.of(42, 43)).containsAtLeast(43, 42).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting.that(LongStream.of(42, 43)).containsAtLeast(43, 42).inOrder());
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}

@Test
Expand Down Expand Up @@ -248,17 +247,19 @@ public void testContainsAtLeastElementsIn_inOrder() throws Exception {

@Test
public void testContainsAtLeastElementsIn_inOrder_fails() throws Exception {
try {
assertThat(LongStream.of(42, 43)).containsAtLeastElementsIn(asList(43L, 42L)).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(LongStream.of(42, 43))
.containsAtLeastElementsIn(asList(43L, 42L))
.inOrder());
assertFailureKeys(
expected,
"required elements were all found, but order was wrong",
"expected order for required elements",
"but was");
assertFailureValue(expected, "expected order for required elements", "[43, 42]");
}

@Test
Expand All @@ -279,13 +280,10 @@ public void testContainsExactly() throws Exception {

@Test
public void testContainsExactly_fails() throws Exception {
try {
assertThat(LongStream.of(42, 43)).containsExactly(42);
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}
AssertionError expected =
expectFailure(whenTesting -> whenTesting.that(LongStream.of(42, 43)).containsExactly(42));
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}

@Test
Expand All @@ -295,13 +293,12 @@ public void testContainsExactly_inOrder() throws Exception {

@Test
public void testContainsExactly_inOrder_fails() throws Exception {
try {
assertThat(LongStream.of(42, 43)).containsExactly(43, 42).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting.that(LongStream.of(42, 43)).containsExactly(43, 42).inOrder());
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}

@Test
Expand All @@ -312,13 +309,12 @@ public void testContainsExactlyElementsIn() throws Exception {

@Test
public void testContainsExactlyElementsIn_fails() throws Exception {
try {
assertThat(LongStream.of(42, 43)).containsExactlyElementsIn(asList(42L));
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting.that(LongStream.of(42, 43)).containsExactlyElementsIn(asList(42L)));
assertFailureKeys(expected, "unexpected (1)", "---", "expected", "but was");
assertFailureValue(expected, "expected", "[42]");
}

@Test
Expand All @@ -336,13 +332,15 @@ public void testContainsExactlyElementsIn_inOrder() throws Exception {

@Test
public void testContainsExactlyElementsIn_inOrder_fails() throws Exception {
try {
assertThat(LongStream.of(42, 43)).containsExactlyElementsIn(asList(43L, 42L)).inOrder();
fail();
} catch (AssertionError expected) {
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(LongStream.of(42, 43))
.containsExactlyElementsIn(asList(43L, 42L))
.inOrder());
assertFailureKeys(expected, "contents match, but order was wrong", "expected", "but was");
assertFailureValue(expected, "expected", "[43, 42]");
}

@Test
Expand Down
Loading

0 comments on commit 3ec9800

Please sign in to comment.