How to use doCommonCheckForMessages method of org.assertj.core.internal.Throwables class

Best Assertj code snippet using org.assertj.core.internal.Throwables.doCommonCheckForMessages

Source:Throwables.java Github

copy

Full Screen

...214 * @throws AssertionError if the actual {@code Throwable} is {@code null}.215 * @throws AssertionError if the message of the actual {@code Throwable} does not contain the given description.216 */217 public void assertHasMessageContainingAll(AssertionInfo info, Throwable actual, CharSequence... values) {218 doCommonCheckForMessages(info, actual, values);219 assertNotNull(info, actual);220 String actualMessage = actual.getMessage();221 Set<CharSequence> notFound = stream(values).filter(value -> actualMessage == null || !actualMessage.contains(value))222 .collect(toCollection(LinkedHashSet::new));223 if (notFound.isEmpty()) return;224 if (notFound.size() == 1 && values.length == 1) {225 throw failures.failure(info, shouldContain(actual, values[0]), actual, values[0]);226 }227 throw failures.failure(info, shouldContain(actual, values, notFound), actual, values);228 }229 /**230 * Asserts that the message of the actual {@code Throwable} does not contain the given content or is {@code null}.231 *232 * @param info contains information about the assertion.233 * @param actual the given {@code Throwable}.234 * @param content the content expected not to be contained in the actual {@code Throwable}'s message.235 * @throws AssertionError if the actual {@code Throwable} is {@code null}.236 * @throws AssertionError if the message of the actual {@code Throwable} contains the given content.237 * @since 3.12.0238 */239 public void assertHasMessageNotContaining(AssertionInfo info, Throwable actual, String content) {240 assertNotNull(info, actual);241 if (actual.getMessage() == null || !actual.getMessage().contains(content)) return;242 throw failures.failure(info, shouldNotContain(actual.getMessage(), content), actual.getMessage(), content);243 }244 /**245 * Asserts that the message of the actual {@code Throwable} does not contain any of the given values or is {@code null}.246 *247 * @param info contains information about the assertion.248 * @param actual the given {@code Throwable}.249 * @param values the contents expected to not be contained in the actual {@code Throwable}'s message.250 * @throws AssertionError if the actual {@code Throwable} is {@code null}.251 * @throws AssertionError if the message of the actual {@code Throwable} does not contain the given description.252 */253 public void assertHasMessageNotContainingAny(AssertionInfo info, Throwable actual, CharSequence... values) {254 doCommonCheckForMessages(info, actual, values);255 String actualMessage = actual.getMessage();256 Set<CharSequence> found = stream(values).filter(value -> actualMessage != null && actualMessage.contains(value))257 .collect(toCollection(LinkedHashSet::new));258 if (found.isEmpty()) return;259 if (found.size() == 1 && values.length == 1) {260 throw failures.failure(info, shouldNotContain(actualMessage, values[0]), actualMessage, values[0]);261 }262 throw failures.failure(info, shouldNotContain(actualMessage, values, found, StandardComparisonStrategy.instance()),263 actualMessage, values);264 }265 /**266 * Asserts that the stack trace of the actual {@code Throwable} contains with the given description.267 *268 * @param info contains information about the assertion.269 * @param actual the given {@code Throwable}.270 * @param description the description expected to be contained in the actual {@code Throwable}'s stack trace.271 * @throws AssertionError if the actual {@code Throwable} is {@code null}.272 * @throws AssertionError if the stack trace of the actual {@code Throwable} does not contain the given description.273 */274 public void assertHasStackTraceContaining(AssertionInfo info, Throwable actual, String description) {275 assertNotNull(info, actual);276 String stackTrace = org.assertj.core.util.Throwables.getStackTrace(actual);277 if (stackTrace != null && stackTrace.contains(description)) return;278 throw failures.failure(info, shouldContain(stackTrace, description));279 }280 /**281 * Asserts that the message of the actual {@code Throwable} matches with the given regular expression.282 *283 * @param info contains information about the assertion.284 * @param actual the given {@code Throwable}.285 * @param regex the regular expression of value expected to be matched the actual {@code Throwable}'s message.286 * @throws AssertionError if the actual {@code Throwable} is {@code null}.287 * @throws AssertionError if the message of the actual {@code Throwable} does not match the given regular expression.288 * @throws NullPointerException if the regex is null289 */290 public void assertHasMessageMatching(AssertionInfo info, Throwable actual, String regex) {291 requireNonNull(regex, "regex must not be null");292 assertNotNull(info, actual);293 if (actual.getMessage() != null && actual.getMessage().matches(regex)) return;294 throw failures.failure(info, shouldHaveMessageMatchingRegex(actual, regex));295 }296 /**297 * Asserts that a sequence of the message of the actual {@code Throwable} matches with the given regular expression (see {@link java.util.regex.Matcher#find()}).298 * The Pattern used under the hood enables the {@link Pattern#DOTALL} mode.299 *300 * @param info contains information about the assertion.301 * @param actual the given {@code Throwable}.302 * @param regex the regular expression expected to be found in the actual {@code Throwable}'s message.303 * @throws AssertionError if the actual {@code Throwable} is {@code null}.304 * @throws AssertionError if the message of the actual {@code Throwable} doesn't contain any sequence matching with the given regular expression305 * @throws NullPointerException if the regex is null306 */307 public void assertHasMessageFindingMatch(AssertionInfo info, Throwable actual, String regex) {308 requireNonNull(regex, "regex must not be null");309 assertNotNull(info, actual);310 Objects.instance().assertNotNull(info, actual.getMessage(), "exception message of actual");311 if (Pattern.compile(regex, Pattern.DOTALL).asPredicate().test(actual.getMessage())) return;312 throw failures.failure(info, shouldHaveMessageFindingMatchRegex(actual, regex));313 }314 /**315 * Asserts that the message of the actual {@code Throwable} ends with the given description.316 *317 * @param info contains information about the assertion.318 * @param actual the given {@code Throwable}.319 * @param description the description expected to end the actual {@code Throwable}'s message.320 * @throws AssertionError if the actual {@code Throwable} is {@code null}.321 * @throws AssertionError if the message of the actual {@code Throwable} does not end with the given description.322 */323 public void assertHasMessageEndingWith(AssertionInfo info, Throwable actual, String description) {324 assertNotNull(info, actual);325 if (actual.getMessage() != null && actual.getMessage().endsWith(description)) return;326 throw failures.failure(info, shouldEndWith(actual.getMessage(), description));327 }328 /**329 * Assert that the cause of actual {@code Throwable} is an instance of the given type.330 *331 * @param info contains information about the assertion.332 * @param actual the given {@code Throwable}.333 * @param type the expected cause type.334 * @throws NullPointerException if given type is {@code null}.335 * @throws AssertionError if the actual {@code Throwable} is {@code null}.336 * @throws AssertionError if the actual {@code Throwable} has no cause.337 * @throws AssertionError if the cause of the actual {@code Throwable} is not an instance of the given type.338 */339 public void assertHasCauseInstanceOf(AssertionInfo info, Throwable actual, Class<? extends Throwable> type) {340 assertNotNull(info, actual);341 checkTypeIsNotNull(type);342 if (type.isInstance(actual.getCause())) return;343 throw failures.failure(info, shouldHaveCauseInstance(actual, type));344 }345 /**346 * Assert that the cause of actual {@code Throwable} is <b>exactly</b> an instance of the given type.347 *348 * @param info contains information about the assertion.349 * @param actual the given {@code Throwable}.350 * @param type the expected cause type.351 * @throws NullPointerException if given type is {@code null}.352 * @throws AssertionError if the actual {@code Throwable} is {@code null}.353 * @throws AssertionError if the actual {@code Throwable} has no cause.354 * @throws AssertionError if the cause of the actual {@code Throwable} is not <b>exactly</b> an instance of the given355 * type.356 */357 public void assertHasCauseExactlyInstanceOf(AssertionInfo info, Throwable actual, Class<? extends Throwable> type) {358 assertNotNull(info, actual);359 checkTypeIsNotNull(type);360 Throwable cause = actual.getCause();361 if (cause != null && type.equals(cause.getClass())) return;362 throw failures.failure(info, shouldHaveCauseExactlyInstance(actual, type));363 }364 /**365 * Assert that the root cause of actual {@code Throwable} is an instance of the given type.366 *367 * @param info contains information about the assertion.368 * @param actual the given {@code Throwable}.369 * @param type the expected cause type.370 * @throws NullPointerException if given type is {@code null}.371 * @throws AssertionError if the actual {@code Throwable} is {@code null}.372 * @throws AssertionError if the actual {@code Throwable} has no cause.373 * @throws AssertionError if the cause of the actual {@code Throwable} is not an instance of the given type.374 */375 public void assertHasRootCauseInstanceOf(AssertionInfo info, Throwable actual, Class<? extends Throwable> type) {376 assertNotNull(info, actual);377 checkTypeIsNotNull(type);378 if (type.isInstance(getRootCause(actual))) return;379 throw failures.failure(info, shouldHaveRootCauseInstance(actual, type));380 }381 /**382 * Assert that the root cause of actual {@code Throwable} is <b>exactly</b> an instance of the given type.383 *384 * @param info contains information about the assertion.385 * @param actual the given {@code Throwable}.386 * @param type the expected cause type.387 * @throws NullPointerException if given type is {@code null}.388 * @throws AssertionError if the actual {@code Throwable} is {@code null}.389 * @throws AssertionError if the actual {@code Throwable} has no cause.390 * @throws AssertionError if the root cause of the actual {@code Throwable} is not <b>exactly</b> an instance of the391 * given type.392 */393 public void assertHasRootCauseExactlyInstanceOf(AssertionInfo info, Throwable actual,394 Class<? extends Throwable> type) {395 assertNotNull(info, actual);396 checkTypeIsNotNull(type);397 Throwable rootCause = getRootCause(actual);398 if (rootCause != null && type.equals(rootCause.getClass())) return;399 throw failures.failure(info, shouldHaveRootCauseExactlyInstance(actual, type));400 }401 public void assertHasNoSuppressedExceptions(AssertionInfo info, Throwable actual) {402 assertNotNull(info, actual);403 Throwable[] suppressed = actual.getSuppressed();404 if (suppressed.length != 0) throw failures.failure(info, shouldHaveNoSuppressedExceptions(actual));405 }406 public void assertHasSuppressedException(AssertionInfo info, Throwable actual,407 Throwable expectedSuppressedException) {408 assertNotNull(info, actual);409 requireNonNull(expectedSuppressedException, "The expected suppressed exception should not be null");410 Throwable[] suppressed = actual.getSuppressed();411 for (Throwable throwable : suppressed) {412 if (compareThrowable(throwable, expectedSuppressedException)) return;413 }414 throw failures.failure(info, shouldHaveSuppressedException(actual, expectedSuppressedException));415 }416 private static void doCommonCheckForMessages(AssertionInfo info, Throwable actual, CharSequence[] values) {417 assertNotNull(info, actual);418 checkIsNotNull(values);419 checkIsNotEmpty(values);420 checkCharSequenceArrayDoesNotHaveNullElements(values);421 }422 private static void assertNotNull(AssertionInfo info, Throwable actual) {423 Objects.instance().assertNotNull(info, actual);424 }425 private static void checkIsNotNull(CharSequence... values) {426 if (values == null) throw arrayOfValuesToLookForIsNull();427 }428 private static void checkIsNotEmpty(CharSequence... values) {429 if (values.length == 0) throw arrayOfValuesToLookForIsEmpty();430 }...

Full Screen

Full Screen

doCommonCheckForMessages

Using AI Code Generation

copy

Full Screen

1@DisplayName("doCommonCheckForMessages method of org.assertj.core.internal.Throwables class")2class Throwables_doCommonCheckForMessages_Test {3 @DisplayName("should throw an AssertionError if the actual exception is null")4 void should_throw_AssertionError_if_actual_is_null() {5 Throwable actual = null;6 String expectedMessage = "expected message";7 AssertionError error = expectAssertionError(() -> throwables.doCommonCheckForMessages(actual, expectedMessage));8 then(error).hasMessage(shouldNotBeNull().create());9 }10 @DisplayName("should throw an AssertionError if the expected message is null")11 void should_throw_AssertionError_if_expected_message_is_null() {12 Throwable actual = new Throwable("actual message");13 String expectedMessage = null;14 AssertionError error = expectAssertionError(() -> throwables.doCommonCheckForMessages(actual, expectedMessage));15 then(error).hasMessage(shouldNotBeNull().create());16 }17 @DisplayName("should throw an AssertionError if the expected message is empty")18 void should_throw_AssertionError_if_expected_message_is_empty() {19 Throwable actual = new Throwable("actual message");20 String expectedMessage = "";21 AssertionError error = expectAssertionError(() -> throwables.doCommonCheckForMessages(actual, expectedMessage));22 then(error).hasMessage(shouldNotBeEmpty().create());23 }24 @DisplayName("should not throw an AssertionError if the actual exception is not null and expected message is not null")25 void should_not_throw_AssertionError_if_actual_is_not_null_and_expected_message_is_not_null() {26 Throwable actual = new Throwable("actual message");27 String expectedMessage = "expected message";28 throwables.doCommonCheckForMessages(actual, expectedMessage);29 }30}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful