Migrate from assertThat(foo).named("foo") to assertWithMessage("foo").that(foo).

(The exact change is slightly different in some cases, like when using custom subjects, but it's still a migration from named(...) to assertWithMessage(...).)

Or, in one case, migrate from calling this.named(...) to overriding actualCustomStringRepresentation().

named(...) is being removed.

This CL may modify the failure messages produced, but all the old information will still be present.

Change-Id: I02219e5c502193e02e2c0abbf8eb9d73c406b0ee
This commit is contained in:
Chris Povirk
2019-05-16 13:00:08 -04:00
parent c01ea3f85b
commit eda92e08c3
4 changed files with 20 additions and 14 deletions

View File

@@ -73,7 +73,11 @@ public abstract class AbstractNotificationTest extends AbstractDaemonTest {
} }
protected static FakeEmailSenderSubject assertThat(FakeEmailSender sender) { protected static FakeEmailSenderSubject assertThat(FakeEmailSender sender) {
return assertAbout(FakeEmailSenderSubject::new).that(sender); return assertAbout(fakeEmailSenders()).that(sender);
}
protected static Subject.Factory<FakeEmailSenderSubject, FakeEmailSender> fakeEmailSenders() {
return FakeEmailSenderSubject::new;
} }
protected void setEmailStrategy(TestAccount account, EmailStrategy strategy) throws Exception { protected void setEmailStrategy(TestAccount account, EmailStrategy strategy) throws Exception {
@@ -142,9 +146,7 @@ public abstract class AbstractNotificationTest extends AbstractDaemonTest {
: header)); : header));
} }
// Return a named subject that displays a human-readable table of return this;
// recipients.
return named(recipientMapToString(recipients, users::emailToName));
} }
private static String recipientMapToString( private static String recipientMapToString(
@@ -205,8 +207,9 @@ public abstract class AbstractNotificationTest extends AbstractDaemonTest {
if (recipients.get(type).contains(email) != expected) { if (recipients.get(type).contains(email) != expected) {
failWithoutActual( failWithoutActual(
fact( fact(
expected ? "should notify" : "shouldn't notify", expected ? "expected to notify" : "expected not to notify",
type + ": " + users.emailToName(email))); type + ": " + users.emailToName(email)),
fact("but notified", recipientMapToString(recipients, users::emailToName)));
} }
if (expected) { if (expected) {
accountedFor.add(email); accountedFor.add(email);

View File

@@ -14,7 +14,7 @@
package com.google.gerrit.testing; package com.google.gerrit.testing;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage;
import com.google.common.util.concurrent.ForwardingExecutorService; import com.google.common.util.concurrent.ForwardingExecutorService;
import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.MoreExecutors;
@@ -57,8 +57,8 @@ public class AssertableExecutorService extends ForwardingExecutorService {
/** Asserts and resets the number of executions this executor observed. */ /** Asserts and resets the number of executions this executor observed. */
public void assertInteractions(int expectedNumInteractions) { public void assertInteractions(int expectedNumInteractions) {
assertThat(numInteractions.get()) assertWithMessage("expectedRunnablesSubmittedOnExecutor")
.named("expectedRunnablesSubmittedOnExecutor") .that(numInteractions.get())
.isEqualTo(expectedNumInteractions); .isEqualTo(expectedNumInteractions);
numInteractions.set(0); numInteractions.set(0);
} }

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.acceptance.server.mail; package com.google.gerrit.acceptance.server.mail;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.gerrit.extensions.api.changes.NotifyHandling.ALL; import static com.google.gerrit.extensions.api.changes.NotifyHandling.ALL;
import static com.google.gerrit.extensions.api.changes.NotifyHandling.NONE; import static com.google.gerrit.extensions.api.changes.NotifyHandling.NONE;
import static com.google.gerrit.extensions.api.changes.NotifyHandling.OWNER; import static com.google.gerrit.extensions.api.changes.NotifyHandling.OWNER;
@@ -1515,15 +1516,16 @@ public class ChangeNotificationsIT extends AbstractNotificationTest {
} }
merge(sc.changeId, sc.owner); merge(sc.changeId, sc.owner);
assertThat(sender) assertWithMessage(name)
.named(name) .about(fakeEmailSenders())
.that(sender)
.sent("merged", sc) .sent("merged", sc)
.cc(sc.reviewer, sc.ccer) .cc(sc.reviewer, sc.ccer)
.cc(sc.reviewerByEmail, sc.ccerByEmail) .cc(sc.reviewerByEmail, sc.ccerByEmail)
.bcc(sc.starrer) .bcc(sc.starrer)
.bcc(ALL_COMMENTS, SUBMITTED_CHANGES) .bcc(ALL_COMMENTS, SUBMITTED_CHANGES)
.noOneElse(); .noOneElse();
assertThat(sender).named(name).didNotSend(); assertWithMessage(name).about(fakeEmailSenders()).that(sender).didNotSend();
} }
} }

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.git; package com.google.gerrit.git;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.gerrit.git.ObjectIds.abbreviateName; import static com.google.gerrit.git.ObjectIds.abbreviateName;
import static com.google.gerrit.testing.GerritJUnit.assertThrows; import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH; import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
@@ -111,8 +112,8 @@ public class ObjectIdsTest {
assertThat(ObjectIds.matchesAbbreviation(ID, "")).isTrue(); assertThat(ObjectIds.matchesAbbreviation(ID, "")).isTrue();
for (int i = 1; i <= OBJECT_ID_STRING_LENGTH; i++) { for (int i = 1; i <= OBJECT_ID_STRING_LENGTH; i++) {
String prefix = ID.name().substring(0, i); String prefix = ID.name().substring(0, i);
assertThat(ObjectIds.matchesAbbreviation(ID, prefix)) assertWithMessage("match %s against %s", ID.name(), prefix)
.named("match %s against %s", ID.name(), prefix) .that(ObjectIds.matchesAbbreviation(ID, prefix))
.isTrue(); .isTrue();
} }