How to use buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues method of org.assertj.core.util.Throwables class

Best Assertj code snippet using org.assertj.core.util.Throwables.buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues

Source:Throwables.java Github

copy

Full Screen

...211 private static <T extends Throwable> T createNewInstanceWithLineNumberInErrorMessage(T error,212 StackTraceElement testStackTraceElement) throws ReflectiveOperationException {213 T errorWithLineNumber = isOpentest4jAssertionFailedError(error)214 ? buildOpentest4jAssertionFailedErrorWithLineNumbers(error, testStackTraceElement)215 : buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues(error, testStackTraceElement);216 errorWithLineNumber.setStackTrace(error.getStackTrace());217 Stream.of(error.getSuppressed()).forEach(errorWithLineNumber::addSuppressed);218 return errorWithLineNumber;219 }220 private static <T extends Throwable> boolean isOpentest4jAssertionFailedError(T error) {221 return isInstanceOf(error, "org.opentest4j.AssertionFailedError");222 }223 private static boolean isInstanceOf(Object object, String className) {224 try {225 Class<?> type = Class.forName(className);226 return type.isInstance(object);227 } catch (ClassNotFoundException e) {228 return false;229 }230 }231 private static <T extends Throwable> T buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues(T error,232 StackTraceElement testStackTraceElement) throws ReflectiveOperationException {233 @SuppressWarnings("unchecked")234 Constructor<? extends T> constructor = (Constructor<? extends T>) error.getClass().getConstructor(String.class,235 Throwable.class);236 return constructor.newInstance(buildErrorMessageWithLineNumber(error.getMessage(), testStackTraceElement), error.getCause());237 }238 private static <T extends Throwable> T buildOpentest4jAssertionFailedErrorWithLineNumbers(T error,239 StackTraceElement testStackTraceElement) throws ReflectiveOperationException {240 // AssertionFailedError has actual and expected fields of type ValueWrapper241 Object actualWrapper = byName("actual").apply(error);242 Object expectedWrapper = byName("expected").apply(error);243 if (actualWrapper != null && expectedWrapper != null) {244 // try to call AssertionFailedError(String message, Object expected, Object actual, Throwable cause)245 try {246 Object actual = byName("value").apply(actualWrapper);247 Object expected = byName("value").apply(expectedWrapper);248 @SuppressWarnings("unchecked")249 Constructor<? extends T> constructor = (Constructor<? extends T>) error.getClass().getConstructor(String.class,250 Object.class,251 Object.class,252 Throwable.class);253 return constructor.newInstance(buildErrorMessageWithLineNumber(error.getMessage(), testStackTraceElement),254 expected,255 actual,256 error.getCause());257 } catch (IntrospectionError e) {258 // fallback to AssertionFailedError(String message, Throwable cause) constructor259 }260 }261 return buildAssertionErrorWithLineNumbersButNoActualOrExpectedValues(error, testStackTraceElement);262 }263 private static String buildErrorMessageWithLineNumber(String originalErrorMessage, StackTraceElement testStackTraceElement) {264 String testClassName = simpleClassNameOf(testStackTraceElement);265 String testName = testStackTraceElement.getMethodName();266 int lineNumber = testStackTraceElement.getLineNumber();267 String atLineNumber = format("at %s.%s(%s.java:%s)", testClassName, testName, testClassName, lineNumber);268 if (originalErrorMessage.contains(atLineNumber)) {269 return originalErrorMessage;270 }271 return format(originalErrorMessage.endsWith(format("%n")) ? "%s%s" : "%s%n%s", originalErrorMessage, atLineNumber);272 }273 private static String simpleClassNameOf(StackTraceElement testStackTraceElement) {274 String className = testStackTraceElement.getClassName();275 return className.substring(className.lastIndexOf('.') + 1);...

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