001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.util; 018 019import org.springframework.util.ObjectUtils; 020 021/** 022 * JUnit independent assertion class. 023 * 024 * @author Lukas Krecan 025 * @author Arjen Poutsma 026 * @since 3.2 027 */ 028public abstract class AssertionErrors { 029 030 /** 031 * Fails a test with the given message. 032 * @param message describes the reason for the failure 033 */ 034 public static void fail(String message) { 035 throw new AssertionError(message); 036 } 037 038 /** 039 * Fails a test with the given message passing along expected and actual 040 * values to be added to the message. 041 * <p>For example given: 042 * <pre class="code"> 043 * assertEquals("Response header [" + name + "]", actual, expected); 044 * </pre> 045 * <p>The resulting message is: 046 * <pre class="code"> 047 * Response header [Accept] expected:<application/json> but was:<text/plain> 048 * </pre> 049 * @param message describes the value that failed the match 050 * @param expected expected value 051 * @param actual actual value 052 */ 053 public static void fail(String message, Object expected, Object actual) { 054 throw new AssertionError(message + " expected:<" + expected + "> but was:<" + actual + ">"); 055 } 056 057 /** 058 * Assert the given condition is {@code true} and raise an 059 * {@link AssertionError} if it is not. 060 * @param message the message 061 * @param condition the condition to test for 062 */ 063 public static void assertTrue(String message, boolean condition) { 064 if (!condition) { 065 fail(message); 066 } 067 } 068 069 /** 070 * Assert two objects are equal raise an {@link AssertionError} if not. 071 * <p>For example: 072 * <pre class="code"> 073 * assertEquals("Response header [" + name + "]", actual, expected); 074 * </pre> 075 * @param message describes the value being checked 076 * @param expected the expected value 077 * @param actual the actual value 078 */ 079 public static void assertEquals(String message, Object expected, Object actual) { 080 if (!ObjectUtils.nullSafeEquals(expected, actual)) { 081 fail(message, ObjectUtils.nullSafeToString(expected), ObjectUtils.nullSafeToString(actual)); 082 } 083 } 084 085}