Skip to content

Commit 7d8a09d

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Migrate off the legacy (stateful, JUnit-specific) ExpectFailure API to the modern (lambda-based) API.
RELNOTES=n/a PiperOrigin-RevId: 733335585
1 parent d9b6ae2 commit 7d8a09d

32 files changed

+4270
-2843
lines changed

core/pom.xml

+26-5
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,32 @@
128128
</plugin>
129129
<plugin>
130130
<artifactId>maven-surefire-plugin</artifactId>
131-
<configuration>
132-
<excludes>
133-
<exclude>**/*GwtTest.java</exclude>
134-
</excludes>
135-
</configuration>
131+
<executions>
132+
<execution>
133+
<id>default-test</id>
134+
<configuration>
135+
<excludes>
136+
<exclude>**/ActualValueInferenceTest.java</exclude>
137+
<exclude>**/*GwtTest.java</exclude>
138+
</excludes>
139+
<systemPropertyVariables>
140+
<!-- Avoid automatically generating "value of" lines based on bytecode: We use one set of tests across multiple environments in our testing inside Google, and most of those environments (e.g., Android, J2CL) don't support "value of" lines. Thus, our tests are simplest if we disable "value of" lines altogether. -->
141+
<com.google.common.truth.disable_infer_description>true</com.google.common.truth.disable_infer_description>
142+
</systemPropertyVariables>
143+
</configuration>
144+
</execution>
145+
<execution>
146+
<id>actualvalueinference-test</id>
147+
<configuration>
148+
<includes>
149+
<include>**/ActualValueInferenceTest.java</include>
150+
</includes>
151+
</configuration>
152+
<goals>
153+
<goal>test</goal>
154+
</goals>
155+
</execution>
156+
</executions>
136157
</plugin>
137158
<plugin>
138159
<groupId>org.codehaus.mojo</groupId>

core/src/test/java/com/google/common/truth/BaseSubjectTestCase.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@
1919

2020
/** Base class for truth subject tests to extend. */
2121
abstract class BaseSubjectTestCase extends PlatformBaseSubjectTestCase {
22-
final void assertFailureKeys(String... keys) {
23-
assertThatFailure().factKeys().containsExactlyElementsIn(keys).inOrder();
22+
static void assertFailureKeys(AssertionError e, String... keys) {
23+
assertThat(e).factKeys().containsExactlyElementsIn(keys).inOrder();
2424
}
2525

26-
final void assertFailureValue(String key, String value) {
27-
assertThatFailure().factValue(key).isEqualTo(value);
26+
static void assertFailureValue(AssertionError e, String key, String value) {
27+
assertThat(e).factValue(key).isEqualTo(value);
2828
}
2929

30-
final void assertFailureValueIndexed(String key, int index, String value) {
31-
assertThatFailure().factValue(key, index).isEqualTo(value);
32-
}
33-
34-
final TruthFailureSubject assertThatFailure() {
35-
return assertThat(expectFailure.getFailure());
30+
static void assertFailureValueIndexed(AssertionError e, String key, int index, String value) {
31+
assertThat(e).factValue(key, index).isEqualTo(value);
3632
}
3733
}

core/src/test/java/com/google/common/truth/BigDecimalSubjectTest.java

+27-24
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.common.truth;
1717

18+
import static com.google.common.truth.ExpectFailure.expectFailure;
1819
import static com.google.common.truth.Truth.assertThat;
1920
import static java.math.BigDecimal.TEN;
2021

@@ -47,28 +48,32 @@ public void isEquivalentAccordingToCompareTo() {
4748
public void isEqualToIgnoringScale_bigDecimal() {
4849
assertThat(TEN).isEqualToIgnoringScale(TEN);
4950
assertThat(TEN).isEqualToIgnoringScale(new BigDecimal(10));
50-
expectFailureWhenTestingThat(TEN).isEqualToIgnoringScale(new BigDecimal(3));
51-
assertFailureKeys("expected", "but was", "(scale is ignored)");
52-
assertFailureValue("expected", "3");
53-
assertFailureValue("but was", "10");
51+
AssertionError e =
52+
expectFailure(
53+
whenTesting -> whenTesting.that(TEN).isEqualToIgnoringScale(new BigDecimal(3)));
54+
assertFailureKeys(e, "expected", "but was", "(scale is ignored)");
55+
assertFailureValue(e, "expected", "3");
56+
assertFailureValue(e, "but was", "10");
5457
}
5558

5659
@Test
5760
public void isEqualToIgnoringScale_int() {
5861
assertThat(TEN).isEqualToIgnoringScale(10);
59-
expectFailureWhenTestingThat(TEN).isEqualToIgnoringScale(3);
60-
assertFailureKeys("expected", "but was", "(scale is ignored)");
61-
assertFailureValue("expected", "3");
62-
assertFailureValue("but was", "10");
62+
AssertionError e =
63+
expectFailure(whenTesting -> whenTesting.that(TEN).isEqualToIgnoringScale(3));
64+
assertFailureKeys(e, "expected", "but was", "(scale is ignored)");
65+
assertFailureValue(e, "expected", "3");
66+
assertFailureValue(e, "but was", "10");
6367
}
6468

6569
@Test
6670
public void isEqualToIgnoringScale_long() {
6771
assertThat(TEN).isEqualToIgnoringScale(10L);
68-
expectFailureWhenTestingThat(TEN).isEqualToIgnoringScale(3L);
69-
assertFailureKeys("expected", "but was", "(scale is ignored)");
70-
assertFailureValue("expected", "3");
71-
assertFailureValue("but was", "10");
72+
AssertionError e =
73+
expectFailure(whenTesting -> whenTesting.that(TEN).isEqualToIgnoringScale(3L));
74+
assertFailureKeys(e, "expected", "but was", "(scale is ignored)");
75+
assertFailureValue(e, "expected", "3");
76+
assertFailureValue(e, "but was", "10");
7277
}
7378

7479
@Test
@@ -77,10 +82,11 @@ public void isEqualToIgnoringScale_string() {
7782
assertThat(TEN).isEqualToIgnoringScale("10.");
7883
assertThat(TEN).isEqualToIgnoringScale("10.0");
7984
assertThat(TEN).isEqualToIgnoringScale("10.00");
80-
expectFailureWhenTestingThat(TEN).isEqualToIgnoringScale("3");
81-
assertFailureKeys("expected", "but was", "(scale is ignored)");
82-
assertFailureValue("expected", "3");
83-
assertFailureValue("but was", "10");
85+
AssertionError e =
86+
expectFailure(whenTesting -> whenTesting.that(TEN).isEqualToIgnoringScale("3"));
87+
assertFailureKeys(e, "expected", "but was", "(scale is ignored)");
88+
assertFailureValue(e, "expected", "3");
89+
assertFailureValue(e, "but was", "10");
8490
}
8591

8692
@Test
@@ -90,13 +96,10 @@ public void isEqualToIgnoringScale_stringWithDecimals() {
9096
assertThat(tenFour).isEqualToIgnoringScale("10.4");
9197
assertThat(tenFour).isEqualToIgnoringScale("10.40");
9298
assertThat(tenFour).isEqualToIgnoringScale("10.400");
93-
expectFailureWhenTestingThat(tenFour).isEqualToIgnoringScale("3.4");
94-
assertFailureKeys("expected", "but was", "(scale is ignored)");
95-
assertFailureValue("expected", "3.4");
96-
assertFailureValue("but was", "10.4");
97-
}
98-
99-
private BigDecimalSubject expectFailureWhenTestingThat(BigDecimal actual) {
100-
return expectFailure.whenTesting().that(actual);
99+
AssertionError e =
100+
expectFailure(whenTesting -> whenTesting.that(tenFour).isEqualToIgnoringScale("3.4"));
101+
assertFailureKeys(e, "expected", "but was", "(scale is ignored)");
102+
assertFailureValue(e, "expected", "3.4");
103+
assertFailureValue(e, "but was", "10.4");
101104
}
102105
}

core/src/test/java/com/google/common/truth/BooleanSubjectTest.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.common.truth;
1717

18+
import static com.google.common.truth.ExpectFailure.expectFailure;
1819
import static com.google.common.truth.Truth.assertThat;
1920

2021
import org.junit.Test;
@@ -36,24 +37,24 @@ public void isTrue() {
3637

3738
@Test
3839
public void nullIsTrueFailing() {
39-
expectFailureWhenTestingThat(null).isTrue();
40-
assertFailureKeys("expected", "but was");
41-
assertFailureValue("expected", "true");
42-
assertFailureValue("but was", "null");
40+
AssertionError e = expectFailure(whenTesting -> whenTesting.that((Boolean) null).isTrue());
41+
assertFailureKeys(e, "expected", "but was");
42+
assertFailureValue(e, "expected", "true");
43+
assertFailureValue(e, "but was", "null");
4344
}
4445

4546
@Test
4647
public void nullIsFalseFailing() {
47-
expectFailureWhenTestingThat(null).isFalse();
48-
assertFailureKeys("expected", "but was");
49-
assertFailureValue("expected", "false");
50-
assertFailureValue("but was", "null");
48+
AssertionError e = expectFailure(whenTesting -> whenTesting.that((Boolean) null).isFalse());
49+
assertFailureKeys(e, "expected", "but was");
50+
assertFailureValue(e, "expected", "false");
51+
assertFailureValue(e, "but was", "null");
5152
}
5253

5354
@Test
5455
public void isTrueFailing() {
55-
expectFailureWhenTestingThat(false).isTrue();
56-
assertFailureKeys("expected to be true");
56+
AssertionError e = expectFailure(whenTesting -> whenTesting.that(false).isTrue());
57+
assertFailureKeys(e, "expected to be true");
5758
}
5859

5960
@Test
@@ -63,11 +64,7 @@ public void isFalse() {
6364

6465
@Test
6566
public void isFalseFailing() {
66-
expectFailureWhenTestingThat(true).isFalse();
67-
assertFailureKeys("expected to be false");
68-
}
69-
70-
private BooleanSubject expectFailureWhenTestingThat(Boolean actual) {
71-
return expectFailure.whenTesting().that(actual);
67+
AssertionError e = expectFailure(whenTesting -> whenTesting.that(true).isFalse());
68+
assertFailureKeys(e, "expected to be false");
7269
}
7370
}

0 commit comments

Comments
 (0)