Skip to content

Commit 55c4571

Browse files
committed
JAVA-2815: Improve transaction test error assertion failure messages
Add the operation name to the assertion messages so that it's clearer which operation is causing the assertion to fire.
1 parent 3358d75 commit 55c4571

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

driver-async/src/test/functional/com/mongodb/async/client/TransactionsTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,18 @@ public void execute() {
285285

286286
assertEquals("Expected operation result differs from actual", expectedResult, actualResult);
287287
}
288-
assertFalse(String.format("Expected error '%s' but none thrown", getErrorContainsField(expectedResult)),
289-
hasErrorContainsField(expectedResult));
290-
assertFalse(String.format("Expected error code '%s' but none thrown", getErrorCodeNameField(expectedResult)),
291-
hasErrorCodeNameField(expectedResult));
288+
assertFalse(String.format("Expected error '%s' but none thrown for operation %s",
289+
getErrorContainsField(expectedResult), operationName), hasErrorContainsField(expectedResult));
290+
assertFalse(String.format("Expected error code '%s' but none thrown for operation %s",
291+
getErrorCodeNameField(expectedResult), operationName), hasErrorCodeNameField(expectedResult));
292292
} catch (RuntimeException e) {
293293
boolean passedAssertion = false;
294294
if (hasErrorLabelsContainField(expectedResult)) {
295295
if (e instanceof MongoException) {
296296
MongoException mongoException = (MongoException) e;
297297
for (String curErrorLabel : getErrorLabelsContainField(expectedResult)) {
298-
assertTrue(String.format("Expected error label '%s but found labels '%s'", curErrorLabel,
299-
mongoException.getErrorLabels()),
298+
assertTrue(String.format("Expected error label '%s but found labels '%s' for operation %s",
299+
curErrorLabel, mongoException.getErrorLabels(), operationName),
300300
mongoException.hasErrorLabel(curErrorLabel));
301301
}
302302
passedAssertion = true;
@@ -306,17 +306,17 @@ public void execute() {
306306
if (e instanceof MongoException) {
307307
MongoException mongoException = (MongoException) e;
308308
for (String curErrorLabel : getErrorLabelsOmitField(expectedResult)) {
309-
assertFalse(String.format("Expected error label '%s omitted but found labels '%s'", curErrorLabel,
310-
mongoException.getErrorLabels()),
309+
assertFalse(String.format("Expected error label '%s omitted but found labels '%s' for operation %s",
310+
curErrorLabel, mongoException.getErrorLabels(), operationName),
311311
mongoException.hasErrorLabel(curErrorLabel));
312312
}
313313
passedAssertion = true;
314314
}
315315
}
316316
if (hasErrorContainsField(expectedResult)) {
317317
String expectedError = getErrorContainsField(expectedResult);
318-
assertTrue(String.format("Expected '%s' but got '%s'", expectedError, e.getMessage()),
319-
e.getMessage().toLowerCase().contains(expectedError.toLowerCase()));
318+
assertTrue(String.format("Expected '%s' but got '%s' for operation %s", expectedError, e.getMessage(),
319+
operationName), e.getMessage().toLowerCase().contains(expectedError.toLowerCase()));
320320
passedAssertion = true;
321321
}
322322
if (hasErrorCodeNameField(expectedResult)) {
@@ -331,7 +331,8 @@ public void execute() {
331331
}
332332
if (!passedAssertion) {
333333
throw e;
334-
} }
334+
}
335+
}
335336
}
336337
} finally {
337338
closeAllSessions();

driver-sync/src/test/functional/com/mongodb/client/TransactionsTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,18 @@ public void shouldPassAllOutcomes() {
273273

274274
assertEquals("Expected operation result differs from actual", expectedResult, actualResult);
275275
}
276-
assertFalse(String.format("Expected error '%s' but none thrown", getErrorContainsField(expectedResult)),
277-
hasErrorContainsField(expectedResult));
278-
assertFalse(String.format("Expected error code '%s' but none thrown", getErrorCodeNameField(expectedResult)),
279-
hasErrorCodeNameField(expectedResult));
276+
assertFalse(String.format("Expected error '%s' but none thrown for operation %s",
277+
getErrorContainsField(expectedResult), operationName), hasErrorContainsField(expectedResult));
278+
assertFalse(String.format("Expected error code '%s' but none thrown for operation %s",
279+
getErrorCodeNameField(expectedResult), operationName), hasErrorCodeNameField(expectedResult));
280280
} catch (RuntimeException e) {
281281
boolean passedAssertion = false;
282282
if (hasErrorLabelsContainField(expectedResult)) {
283283
if (e instanceof MongoException) {
284284
MongoException mongoException = (MongoException) e;
285285
for (String curErrorLabel : getErrorLabelsContainField(expectedResult)) {
286-
assertTrue(String.format("Expected error label '%s but found labels '%s'", curErrorLabel,
287-
mongoException.getErrorLabels()),
286+
assertTrue(String.format("Expected error label '%s but found labels '%s' for operation %s",
287+
curErrorLabel, mongoException.getErrorLabels(), operationName),
288288
mongoException.hasErrorLabel(curErrorLabel));
289289
}
290290
passedAssertion = true;
@@ -294,17 +294,17 @@ public void shouldPassAllOutcomes() {
294294
if (e instanceof MongoException) {
295295
MongoException mongoException = (MongoException) e;
296296
for (String curErrorLabel : getErrorLabelsOmitField(expectedResult)) {
297-
assertFalse(String.format("Expected error label '%s omitted but found labels '%s'", curErrorLabel,
298-
mongoException.getErrorLabels()),
297+
assertFalse(String.format("Expected error label '%s omitted but found labels '%s' for operation %s",
298+
curErrorLabel, mongoException.getErrorLabels(), operationName),
299299
mongoException.hasErrorLabel(curErrorLabel));
300300
}
301301
passedAssertion = true;
302302
}
303303
}
304304
if (hasErrorContainsField(expectedResult)) {
305305
String expectedError = getErrorContainsField(expectedResult);
306-
assertTrue(String.format("Expected '%s' but got '%s'", expectedError, e.getMessage()),
307-
e.getMessage().toLowerCase().contains(expectedError.toLowerCase()));
306+
assertTrue(String.format("Expected '%s' but got '%s' for operation %s", expectedError, e.getMessage(),
307+
operationName), e.getMessage().toLowerCase().contains(expectedError.toLowerCase()));
308308
passedAssertion = true;
309309
}
310310
if (hasErrorCodeNameField(expectedResult)) {

0 commit comments

Comments
 (0)