All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.jupiter.api.Assertions Maven / Gradle / Ivy

There is a newer version: 5.11.4
Show newest version
/*
 * Copyright 2015-2016 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.junit.jupiter.api;

import static java.util.stream.Collectors.joining;
import static org.junit.platform.commons.meta.API.Usage.Experimental;
import static org.junit.platform.commons.meta.API.Usage.Maintained;
import static org.junit.platform.commons.util.ReflectionUtils.isArray;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

import org.junit.platform.commons.meta.API;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.commons.util.StringUtils;
import org.opentest4j.AssertionFailedError;
import org.opentest4j.MultipleFailuresError;

/**
 * {@code Assertions} is a collection of utility methods that support
 * asserting conditions in tests. A failed assertion will
 * always throw {@link AssertionFailedError} or a subclass thereof.
 *
 * @since 5.0
 * @see AssertionFailedError
 * @see Assumptions
 */
@API(Maintained)
public final class Assertions {

	private Assertions() {
		/* no-op */
	}

	// --- fail ----------------------------------------------------------

	/**
	 * Fails a test with the given failure {@code message}.
	 */
	public static void fail(String message) {
		throw new AssertionFailedError(message);
	}

	/**
	 * Fails a test with the failure message retrieved from the
	 * given {@code messageSupplier}.
	 */
	public static void fail(Supplier messageSupplier) {
		fail(nullSafeGet(messageSupplier));
	}

	// --- assertTrue ----------------------------------------------------

	/**
	 * Asserts that the supplied {@code condition} is {@code true}.
	 */
	public static void assertTrue(boolean condition) {
		assertTrue(() -> condition, () -> null);
	}

	/**
	 * Asserts that the supplied {@code condition} is {@code true}.
	 * 

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertTrue(boolean condition, Supplier messageSupplier) { assertTrue(() -> condition, messageSupplier); } /** * Asserts that the boolean condition supplied by {@code booleanSupplier} is {@code true}. */ public static void assertTrue(BooleanSupplier booleanSupplier) { assertTrue(booleanSupplier, () -> null); } /** * Asserts that the boolean condition supplied by {@code booleanSupplier} is {@code true}. *

Fails with the supplied failure {@code message}. */ public static void assertTrue(BooleanSupplier booleanSupplier, String message) { assertTrue(booleanSupplier, () -> message); } /** * Asserts that the supplied {@code condition} is {@code true}. *

Fails with the supplied failure {@code message}. */ public static void assertTrue(boolean condition, String message) { assertTrue(() -> condition, () -> message); } /** * Asserts that the boolean condition supplied by {@code booleanSupplier} is {@code true}. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertTrue(BooleanSupplier booleanSupplier, Supplier messageSupplier) { if (!booleanSupplier.getAsBoolean()) { fail(messageSupplier); } } // --- assertFalse --------------------------------------------------- /** * Asserts that the supplied {@code condition} is not {@code true}. */ public static void assertFalse(boolean condition) { assertFalse(() -> condition, () -> null); } /** * Asserts that the supplied {@code condition} is not {@code true}. *

Fails with the supplied failure {@code message}. */ public static void assertFalse(boolean condition, String message) { assertFalse(() -> condition, () -> message); } /** * Asserts that the supplied {@code condition} is not {@code true}. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertFalse(boolean condition, Supplier messageSupplier) { assertFalse(() -> condition, messageSupplier); } /** * Asserts that the boolean condition supplied by {@code booleanSupplier} is not {@code true}. */ public static void assertFalse(BooleanSupplier booleanSupplier) { assertFalse(booleanSupplier, () -> null); } /** * Asserts that the boolean condition supplied by {@code booleanSupplier} is not {@code true}. *

Fails with the supplied failure {@code message}. */ public static void assertFalse(BooleanSupplier booleanSupplier, String message) { assertFalse(booleanSupplier, () -> message); } /** * Asserts that the boolean condition supplied by {@code booleanSupplier} is not {@code true}. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertFalse(BooleanSupplier booleanSupplier, Supplier messageSupplier) { if (booleanSupplier.getAsBoolean()) { fail(messageSupplier); } } // --- assertNull ---------------------------------------------------- /** * Asserts that {@code actual} is {@code null}. */ public static void assertNull(Object actual) { assertNull(actual, () -> null); } /** * Asserts that {@code actual} is {@code null}. *

Fails with the supplied failure {@code message}. */ public static void assertNull(Object actual, String message) { assertNull(actual, () -> message); } /** * Asserts that {@code actual} is {@code null}. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertNull(Object actual, Supplier messageSupplier) { if (actual != null) { failNotNull(actual, nullSafeGet(messageSupplier)); } } // --- assertNotNull ------------------------------------------------- /** * Asserts that {@code actual} is not {@code null}. */ public static void assertNotNull(Object actual) { assertNotNull(actual, () -> null); } /** * Asserts that {@code actual} is not {@code null}. *

Fails with the supplied failure {@code message}. */ public static void assertNotNull(Object actual, String message) { assertNotNull(actual, () -> message); } /** * Asserts that {@code actual} is not {@code null}. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertNotNull(Object actual, Supplier messageSupplier) { if (actual == null) { failNull(nullSafeGet(messageSupplier)); } } // --- assertEquals ------------------------------------------------- /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(short expected, short actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(short expected, short actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(short expected, short actual, Supplier messageSupplier) { if (expected != actual) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(byte expected, byte actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(byte expected, byte actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(byte expected, byte actual, Supplier messageSupplier) { if (expected != actual) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(int expected, int actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(int expected, int actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(int expected, int actual, Supplier messageSupplier) { if (expected != actual) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(long expected, long actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(long expected, long actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(long expected, long actual, Supplier messageSupplier) { if (expected != actual) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(char expected, char actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. */ public static void assertEquals(char expected, char actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(char expected, char actual, Supplier messageSupplier) { if (expected != actual) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*/ public static void assertEquals(float expected, float actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*/ public static void assertEquals(float expected, float actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(float expected, float actual, Supplier messageSupplier) { if (!floatsAreEqual(expected, actual)) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*/ public static void assertEquals(float expected, float actual, float delta) { assertEquals(expected, actual, delta, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*/ public static void assertEquals(float expected, float actual, float delta, String message) { assertEquals(expected, actual, delta, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(float expected, float actual, float delta, Supplier messageSupplier) { if (!floatsAreEqual(expected, actual, delta)) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*/ public static void assertEquals(double expected, double actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*/ public static void assertEquals(double expected, double actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(double expected, double actual, Supplier messageSupplier) { if (!doublesAreEqual(expected, actual)) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*/ public static void assertEquals(double expected, double actual, double delta) { assertEquals(expected, actual, delta, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*/ public static void assertEquals(double expected, double actual, double delta, String message) { assertEquals(expected, actual, delta, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertEquals(double expected, double actual, double delta, Supplier messageSupplier) { if (!doublesAreEqual(expected, actual, delta)) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } /** * Asserts that {@code expected} and {@code actual} are equal. *

If both are {@code null}, they are considered equal. * * @see Object#equals(Object) */ public static void assertEquals(Object expected, Object actual) { assertEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are equal. *

If both are {@code null}, they are considered equal. *

Fails with the supplied failure {@code message}. * * @see Object#equals(Object) */ public static void assertEquals(Object expected, Object actual, String message) { assertEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are equal. *

If both are {@code null}, they are considered equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. * * @see Object#equals(Object) */ public static void assertEquals(Object expected, Object actual, Supplier messageSupplier) { if (!objectsAreEqual(expected, actual)) { failNotEqual(expected, actual, nullSafeGet(messageSupplier)); } } // --- assertArrayEquals ------------------------------------------------- /** * Asserts that {@code expected} and {@code actual} boolean arrays are equal. *

If both are {@code null}, they are considered equal. */ public static void assertArrayEquals(boolean[] expected, boolean[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} boolean arrays are equal. *

If both are {@code null}, they are considered equal. *

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(boolean[] expected, boolean[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} boolean arrays are equal. *

If both are {@code null}, they are considered equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(boolean[] expected, boolean[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} char arrays are equal. *

If both are {@code null}, they are considered equal. */ public static void assertArrayEquals(char[] expected, char[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} char arrays are equal. *

If both are {@code null}, they are considered equal. *

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(char[] expected, char[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} char arrays are equal. *

If both are {@code null}, they are considered equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(char[] expected, char[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} byte arrays are equal. *

If both are {@code null}, they are considered equal. */ public static void assertArrayEquals(byte[] expected, byte[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} byte arrays are equal. *

If both are {@code null}, they are considered equal. *

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(byte[] expected, byte[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} byte arrays are equal. *

If both are {@code null}, they are considered equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(byte[] expected, byte[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} short arrays are equal. *

If both are {@code null}, they are considered equal. */ public static void assertArrayEquals(short[] expected, short[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} short arrays are equal. *

If both are {@code null}, they are considered equal. *

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(short[] expected, short[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} short arrays are equal. *

If both are {@code null}, they are considered equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(short[] expected, short[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} int arrays are equal. *

If both are {@code null}, they are considered equal. */ public static void assertArrayEquals(int[] expected, int[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} int arrays are equal. *

If both are {@code null}, they are considered equal. *

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(int[] expected, int[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} int arrays are equal. *

If both are {@code null}, they are considered equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(int[] expected, int[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} long arrays are equal. *

If both are {@code null}, they are considered equal. */ public static void assertArrayEquals(long[] expected, long[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} long arrays are equal. *

If both are {@code null}, they are considered equal. *

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(long[] expected, long[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} long arrays are equal. *

If both are {@code null}, they are considered equal. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(long[] expected, long[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} float arrays are equal. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*/ public static void assertArrayEquals(float[] expected, float[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} float arrays are equal. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(float[] expected, float[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} float arrays are equal. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(float[] expected, float[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} float arrays are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*/ public static void assertArrayEquals(float[] expected, float[] actual, float delta) { assertArrayEquals(expected, actual, delta, () -> null); } /** * Asserts that {@code expected} and {@code actual} float arrays are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(float[] expected, float[] actual, float delta, String message) { assertArrayEquals(expected, actual, delta, () -> message); } /** * Asserts that {@code expected} and {@code actual} float arrays are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Float#equals(Object)} and * {@link Float#compare(float, float)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(float[] expected, float[] actual, float delta, Supplier messageSupplier) { assertArrayEquals(expected, actual, delta, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} double arrays are equal. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*/ public static void assertArrayEquals(double[] expected, double[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} double arrays are equal. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(double[] expected, double[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} double arrays are equal. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(double[] expected, double[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} double arrays are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*/ public static void assertArrayEquals(double[] expected, double[] actual, double delta) { assertArrayEquals(expected, actual, delta, () -> null); } /** * Asserts that {@code expected} and {@code actual} double arrays are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*

Fails with the supplied failure {@code message}. */ public static void assertArrayEquals(double[] expected, double[] actual, double delta, String message) { assertArrayEquals(expected, actual, delta, () -> message); } /** * Asserts that {@code expected} and {@code actual} double arrays are equal within the given {@code delta}. *

Equality imposed by this method is consistent with {@link Double#equals(Object)} and * {@link Double#compare(double, double)}.

*

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertArrayEquals(double[] expected, double[] actual, double delta, Supplier messageSupplier) { assertArrayEquals(expected, actual, delta, null, messageSupplier); } /** * Asserts that {@code expected} and {@code actual} object arrays are deeply equal. *

If both are {@code null}, they are considered equal. *

Nested float arrays are checked as in {@link #assertEquals(float, float)}. *

Nested double arrays are checked as in {@link #assertEquals(double, double)}. * * @see Objects#equals(Object, Object) * @see Arrays#deepEquals(Object[], Object[]) */ public static void assertArrayEquals(Object[] expected, Object[] actual) { assertArrayEquals(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} object arrays are deeply equal. *

If both are {@code null}, they are considered equal. *

Nested float arrays are checked as in {@link #assertEquals(float, float)}. *

Nested double arrays are checked as in {@link #assertEquals(double, double)}. *

Fails with the supplied failure {@code message}. * * @see Objects#equals(Object, Object) * @see Arrays#deepEquals(Object[], Object[]) */ public static void assertArrayEquals(Object[] expected, Object[] actual, String message) { assertArrayEquals(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} object arrays are deeply equal. *

If both are {@code null}, they are considered equal. *

Nested float arrays are checked as in {@link #assertEquals(float, float)}. *

Nested double arrays are checked as in {@link #assertEquals(double, double)}. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. * * @see Objects#equals(Object, Object) * @see Arrays#deepEquals(Object[], Object[]) */ public static void assertArrayEquals(Object[] expected, Object[] actual, Supplier messageSupplier) { assertArrayEquals(expected, actual, new ArrayDeque<>(), messageSupplier); } // --- assertNotEquals ------------------------------------------------- /** * Asserts that {@code expected} and {@code actual} are not equal. *

Fails if both are {@code null}. * * @see Object#equals(Object) */ public static void assertNotEquals(Object unexpected, Object actual) { assertNotEquals(unexpected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} are not equal. *

Fails if both are {@code null}. *

Fails with the supplied failure {@code message}. * * @see Object#equals(Object) */ public static void assertNotEquals(Object unexpected, Object actual, String message) { assertNotEquals(unexpected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} are not equal. *

Fails if both are {@code null}. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. * * @see Object#equals(Object) */ public static void assertNotEquals(Object unexpected, Object actual, Supplier messageSupplier) { if (objectsAreEqual(unexpected, actual)) { failEqual(actual, nullSafeGet(messageSupplier)); } } // --- assertSame ---------------------------------------------------- /** * Asserts that {@code expected} and {@code actual} refer to the same object. */ public static void assertSame(Object expected, Object actual) { assertSame(expected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} refer to the same object. *

Fails with the supplied failure {@code message}. */ public static void assertSame(Object expected, Object actual, String message) { assertSame(expected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} refer to the same object. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertSame(Object expected, Object actual, Supplier messageSupplier) { if (expected != actual) { failNotSame(expected, actual, nullSafeGet(messageSupplier)); } } // --- assertNotSame ------------------------------------------------- /** * Asserts that {@code expected} and {@code actual} do not refer to the same object. */ public static void assertNotSame(Object unexpected, Object actual) { assertNotSame(unexpected, actual, () -> null); } /** * Asserts that {@code expected} and {@code actual} do not refer to the same object. *

Fails with the supplied failure {@code message}. */ public static void assertNotSame(Object unexpected, Object actual, String message) { assertNotSame(unexpected, actual, () -> message); } /** * Asserts that {@code expected} and {@code actual} do not refer to the same object. *

If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}. */ public static void assertNotSame(Object unexpected, Object actual, Supplier messageSupplier) { if (unexpected == actual) { failSame(actual, nullSafeGet(messageSupplier)); } } // --- assertAll ----------------------------------------------------- /** * Asserts that all supplied {@code executables} do not throw an * {@link AssertionError}. * *

If any supplied {@link Executable} throws an {@code AssertionError}, all remaining * {@code executables} will still be executed, and all failures will be aggregated * and reported in a {@link MultipleFailuresError}. However, if an {@code executable} * throws an exception that is not an {@code AssertionError}, execution will halt * immediately, and the exception will be rethrown as is but * {@link ExceptionUtils#throwAsUncheckedException masked} as an unchecked exception. * * @see #assertAll(String, Executable...) */ @API(Experimental) public static void assertAll(Executable... executables) throws MultipleFailuresError { assertAll(null, executables); } /** * Asserts that all supplied {@code executables} do not throw an * {@link AssertionError}. * *

If any supplied {@link Executable} throws an {@code AssertionError}, all remaining * {@code executables} will still be executed, and all failures will be aggregated * and reported in a {@link MultipleFailuresError}. However, if an {@code executable} * throws an exception that is not an {@code AssertionError}, execution will halt * immediately, and the exception will be rethrown as is but * {@link ExceptionUtils#throwAsUncheckedException masked} as an unchecked exception. * *

The supplied {@code heading} will be included in the message string for the * {@link MultipleFailuresError}. */ @API(Experimental) public static void assertAll(String heading, Executable... executables) throws MultipleFailuresError { MultipleFailuresError multipleFailuresError = new MultipleFailuresError(heading); for (Executable executable : executables) { try { executable.execute(); } catch (AssertionError assertionError) { multipleFailuresError.addFailure(assertionError); } catch (Throwable t) { throw ExceptionUtils.throwAsUncheckedException(t); } } if (multipleFailuresError.hasFailures()) { throw multipleFailuresError; } } // --- assert exceptions --------------------------------------------- /** * Asserts that execution of the supplied {@code executable} throws * an exception of the {@code expectedType}. * *

If no exception is thrown, or if an exception of a different type is thrown, * this method will fail. * *

Use {@link #assertThrows} if you do not want to perform additional checks on * the exception instance. Otherwise use {@link #expectThrows}. */ public static void assertThrows(Class expectedType, Executable executable) { expectThrows(expectedType, executable); } /** * Asserts that execution of the supplied {@code executable} throws * an exception of the {@code expectedType}, and returns the exception. * *

If no exception is thrown or if an exception of a different type is thrown, * this method will fail. * *

Use {@link #expectThrows} if you want to perform additional checks on the exception instance. * Otherwise use {@link #assertThrows}.

*/ @SuppressWarnings("unchecked") public static T expectThrows(Class expectedType, Executable executable) { try { executable.execute(); } catch (Throwable actualException) { if (expectedType.isInstance(actualException)) { return (T) actualException; } else { String message = Assertions.format(expectedType.getName(), actualException.getClass().getName(), "Unexpected exception type thrown"); throw new AssertionFailedError(message, actualException); } } throw new AssertionFailedError( String.format("Expected %s to be thrown, but nothing was thrown.", expectedType.getName())); } // --- assertArrayEquals helpers ------------------------------------- private static void assertArrayEquals(boolean[] expected, boolean[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(char[] expected, char[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(byte[] expected, byte[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(short[] expected, short[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(int[] expected, int[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(long[] expected, long[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(float[] expected, float[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (!floatsAreEqual(expected[i], actual[i])) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(float[] expected, float[] actual, float delta, Deque indexes, Supplier messageSupplier) { assertValidDelta(delta); if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (!floatsAreEqual(expected[i], actual[i], delta)) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(double[] expected, double[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (!doublesAreEqual(expected[i], actual[i])) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(double[] expected, double[] actual, double delta, Deque indexes, Supplier messageSupplier) { assertValidDelta(delta); if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { if (!doublesAreEqual(expected[i], actual[i], delta)) { failArraysNotEqual(expected[i], actual[i], nullSafeIndexes(indexes, i), messageSupplier); } } } private static void assertArrayEquals(Object[] expected, Object[] actual, Deque indexes, Supplier messageSupplier) { if (expected == actual) { return; } assertArraysNotNull(expected, actual, indexes, messageSupplier); assertArraysHaveSameLength(expected.length, actual.length, indexes, messageSupplier); for (int i = 0; i < expected.length; i++) { Object expectedElement = expected[i]; Object actualElement = actual[i]; if (expectedElement == actualElement) { continue; } indexes.addLast(i); assertArrayElementsEqual(expectedElement, actualElement, indexes, messageSupplier); indexes.removeLast(); } } private static void assertArrayElementsEqual(Object expected, Object actual, Deque indexes, Supplier messageSupplier) { if (expected instanceof Object[] && actual instanceof Object[]) { assertArrayEquals((Object[]) expected, (Object[]) actual, indexes, messageSupplier); } else if (expected instanceof byte[] && actual instanceof byte[]) { assertArrayEquals((byte[]) expected, (byte[]) actual, indexes, messageSupplier); } else if (expected instanceof short[] && actual instanceof short[]) { assertArrayEquals((short[]) expected, (short[]) actual, indexes, messageSupplier); } else if (expected instanceof int[] && actual instanceof int[]) { assertArrayEquals((int[]) expected, (int[]) actual, indexes, messageSupplier); } else if (expected instanceof long[] && actual instanceof long[]) { assertArrayEquals((long[]) expected, (long[]) actual, indexes, messageSupplier); } else if (expected instanceof char[] && actual instanceof char[]) { assertArrayEquals((char[]) expected, (char[]) actual, indexes, messageSupplier); } else if (expected instanceof float[] && actual instanceof float[]) { assertArrayEquals((float[]) expected, (float[]) actual, indexes, messageSupplier); } else if (expected instanceof double[] && actual instanceof double[]) { assertArrayEquals((double[]) expected, (double[]) actual, indexes, messageSupplier); } else if (expected instanceof boolean[] && actual instanceof boolean[]) { assertArrayEquals((boolean[]) expected, (boolean[]) actual, indexes, messageSupplier); } else if (!Objects.equals(expected, actual)) { if (expected == null && isArray(actual)) { failExpectedArrayIsNull(indexes, messageSupplier); } else if (isArray(expected) && actual == null) { failActualArrayIsNull(indexes, messageSupplier); } else { failArraysNotEqual(expected, actual, indexes, messageSupplier); } } } private static void assertArraysNotNull(Object expected, Object actual, Deque indexes, Supplier messageSupplier) { if (expected == null) { failExpectedArrayIsNull(indexes, messageSupplier); } if (actual == null) { failActualArrayIsNull(indexes, messageSupplier); } } private static void failExpectedArrayIsNull(Deque indexes, Supplier messageSupplier) { fail(buildPrefix(nullSafeGet(messageSupplier)) + "expected array was " + formatIndexes(indexes)); } private static void failActualArrayIsNull(Deque indexes, Supplier messageSupplier) { fail(buildPrefix(nullSafeGet(messageSupplier)) + "actual array was " + formatIndexes(indexes)); } private static void assertArraysHaveSameLength(int expected, int actual, Deque indexes, Supplier messageSupplier) { if (expected != actual) { String prefix = buildPrefix(nullSafeGet(messageSupplier)); String message = "array lengths differ" + formatIndexes(indexes) + ", expected: <" + expected + "> but was: <" + actual + ">"; fail(prefix + message); } } private static void failArraysNotEqual(Object expected, Object actual, Deque indexes, Supplier messageSupplier) { String prefix = buildPrefix(nullSafeGet(messageSupplier)); String message = "array contents differ" + formatIndexes(indexes) + ", " + formatValues(expected, actual); fail(prefix + message); } private static Deque nullSafeIndexes(Deque indexes, int newIndex) { Deque result = (indexes != null ? indexes : new ArrayDeque<>()); result.addLast(newIndex); return result; } private static String formatIndexes(Deque indexes) { if (indexes == null || indexes.isEmpty()) { return ""; } String indexesString = indexes.stream().map(Object::toString).collect(joining("][", "[", "]")); return " at index " + indexesString; } // ------------------------------------------------------------------- private static void failEqual(Object actual, String message) { fail(buildPrefix(message) + "expected: not equal but was: <" + actual + ">"); } private static void failNull(String message) { fail(buildPrefix(message) + "expected: not "); } private static void failNotNull(Object actual, String message) { fail(buildPrefix(message) + "expected: but was: <" + actual + ">", null, actual); } private static void failSame(Object actual, String message) { fail(buildPrefix(message) + "expected: not same but was: <" + actual + ">"); } private static void failNotSame(Object expected, Object actual, String message) { fail(format(expected, actual, message), expected, actual); } private static void failNotEqual(Object expected, Object actual, String message) { fail(format(expected, actual, message), expected, actual); } private static void fail(String message, Object expected, Object actual) { throw new AssertionFailedError(message, expected, actual); } private static String format(Object expected, Object actual, String message) { return buildPrefix(message) + formatValues(expected, actual); } private static String formatValues(Object expected, Object actual) { String expectedString = String.valueOf(expected); String actualString = String.valueOf(actual); if (expectedString.equals(actualString)) { return "expected: " + formatClassAndValue(expected, expectedString) + " but was: " + formatClassAndValue(actual, actualString); } else { return "expected: <" + expectedString + "> but was: <" + actualString + ">"; } } private static String formatClassAndValue(Object value, String valueString) { String className = (value == null ? "null" : value.getClass().getName()); String hash = (value == null ? "" : "@" + Integer.toHexString(System.identityHashCode(value))); return className + hash + "<" + valueString + ">"; } private static String buildPrefix(String message) { return (StringUtils.isNotBlank(message) ? message + " ==> " : ""); } private static String nullSafeGet(Supplier messageSupplier) { return (messageSupplier != null ? messageSupplier.get() : null); } private static boolean objectsAreEqual(Object obj1, Object obj2) { if (obj1 == null) { return (obj2 == null); } else { return obj1.equals(obj2); } } private static boolean floatsAreEqual(float value1, float value2) { return Float.floatToIntBits(value1) == Float.floatToIntBits(value2); } private static boolean floatsAreEqual(float value1, float value2, float delta) { assertValidDelta(delta); return floatsAreEqual(value1, value2) || Math.abs(value1 - value2) <= delta; } private static void assertValidDelta(float delta) { if (Float.isNaN(delta) || delta <= 0.0) { failIllegalDelta(String.valueOf(delta)); } } private static boolean doublesAreEqual(double value1, double value2) { return Double.doubleToLongBits(value1) == Double.doubleToLongBits(value2); } private static boolean doublesAreEqual(double value1, double value2, double delta) { assertValidDelta(delta); return doublesAreEqual(value1, value2) || Math.abs(value1 - value2) <= delta; } private static void assertValidDelta(double delta) { if (Double.isNaN(delta) || delta <= 0.0) { failIllegalDelta(String.valueOf(delta)); } } private static void failIllegalDelta(String delta) { fail("positive delta expected but was: <" + delta + ">"); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy