1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.util;
import org.springframework.util.ObjectUtils;
/**
* JUnit independent assertion class.
*
* @author Lukas Krecan
* @author Arjen Poutsma
* @since 3.2
*/
public abstract class AssertionErrors {
/**
* Fails a test with the given message.
* @param message describes the reason for the failure
*/
public static void fail(String message) {
throw new AssertionError(message);
}
/**
* Fails a test with the given message passing along expected and actual
* values to be added to the message.
* <p>For example given:
* <pre class="code">
* assertEquals("Response header [" + name + "]", actual, expected);
* </pre>
* <p>The resulting message is:
* <pre class="code">
* Response header [Accept] expected:<application/json> but was:<text/plain>
* </pre>
* @param message describes the value that failed the match
* @param expected expected value
* @param actual actual value
*/
public static void fail(String message, Object expected, Object actual) {
throw new AssertionError(message + " expected:<" + expected + "> but was:<" + actual + ">");
}
/**
* Assert the given condition is {@code true} and raise an
* {@link AssertionError} if it is not.
* @param message the message
* @param condition the condition to test for
*/
public static void assertTrue(String message, boolean condition) {
if (!condition) {
fail(message);
}
}
/**
* Assert two objects are equal raise an {@link AssertionError} if not.
* <p>For example:
* <pre class="code">
* assertEquals("Response header [" + name + "]", actual, expected);
* </pre>
* @param message describes the value being checked
* @param expected the expected value
* @param actual the actual value
*/
public static void assertEquals(String message, Object expected, Object actual) {
if (!ObjectUtils.nullSafeEquals(expected, actual)) {
fail(message, ObjectUtils.nullSafeToString(expected), ObjectUtils.nullSafeToString(actual));
}
}
}
|