程序包 org.junit

类 Assert


  • public class Assert
    extends Object
    A set of assertion methods useful for writing tests. Only failed assertions are recorded. These methods can be used directly: Assert.assertEquals(...), however, they read better if they are referenced through static import:
     import static org.junit.Assert.*;
        ...
        assertEquals(...);
     
    从以下版本开始:
    4.0
    另请参阅:
    AssertionError
    • 构造器概要

      构造器 
      限定符构造器说明
      protected Assert()
      Protect constructor since it is a static only class
    • 方法概要

      所有方法 静态方法 具体方法 已过时的方法 
      修饰符和类型方法说明
      static voidassertArrayEquals​(boolean[] expecteds, boolean[] actuals)
      Asserts that two boolean arrays are equal.
      static voidassertArrayEquals​(byte[] expecteds, byte[] actuals)
      Asserts that two byte arrays are equal.
      static voidassertArrayEquals​(char[] expecteds, char[] actuals)
      Asserts that two char arrays are equal.
      static voidassertArrayEquals​(double[] expecteds, double[] actuals, double delta)
      Asserts that two double arrays are equal.
      static voidassertArrayEquals​(float[] expecteds, float[] actuals, float delta)
      Asserts that two float arrays are equal.
      static voidassertArrayEquals​(int[] expecteds, int[] actuals)
      Asserts that two int arrays are equal.
      static voidassertArrayEquals​(long[] expecteds, long[] actuals)
      Asserts that two long arrays are equal.
      static voidassertArrayEquals​(short[] expecteds, short[] actuals)
      Asserts that two short arrays are equal.
      static voidassertArrayEquals​(Object[] expecteds, Object[] actuals)
      Asserts that two object arrays are equal.
      static voidassertArrayEquals​(String message, boolean[] expecteds, boolean[] actuals)
      Asserts that two boolean arrays are equal.
      static voidassertArrayEquals​(String message, byte[] expecteds, byte[] actuals)
      Asserts that two byte arrays are equal.
      static voidassertArrayEquals​(String message, char[] expecteds, char[] actuals)
      Asserts that two char arrays are equal.
      static voidassertArrayEquals​(String message, double[] expecteds, double[] actuals, double delta)
      Asserts that two double arrays are equal.
      static voidassertArrayEquals​(String message, float[] expecteds, float[] actuals, float delta)
      Asserts that two float arrays are equal.
      static voidassertArrayEquals​(String message, int[] expecteds, int[] actuals)
      Asserts that two int arrays are equal.
      static voidassertArrayEquals​(String message, long[] expecteds, long[] actuals)
      Asserts that two long arrays are equal.
      static voidassertArrayEquals​(String message, short[] expecteds, short[] actuals)
      Asserts that two short arrays are equal.
      static voidassertArrayEquals​(String message, Object[] expecteds, Object[] actuals)
      Asserts that two object arrays are equal.
      static voidassertEquals​(double expected, double actual)
      已过时。
      Use assertEquals(double expected, double actual, double delta) instead
      static voidassertEquals​(double expected, double actual, double delta)
      Asserts that two doubles are equal to within a positive delta.
      static voidassertEquals​(float expected, float actual, float delta)
      Asserts that two floats are equal to within a positive delta.
      static voidassertEquals​(long expected, long actual)
      Asserts that two longs are equal.
      static voidassertEquals​(Object[] expecteds, Object[] actuals)
      已过时。
      use assertArrayEquals
      static voidassertEquals​(Object expected, Object actual)
      Asserts that two objects are equal.
      static voidassertEquals​(String message, double expected, double actual)
      已过时。
      Use assertEquals(String message, double expected, double actual, double delta) instead
      static voidassertEquals​(String message, double expected, double actual, double delta)
      Asserts that two doubles are equal to within a positive delta.
      static voidassertEquals​(String message, float expected, float actual, float delta)
      Asserts that two floats are equal to within a positive delta.
      static voidassertEquals​(String message, long expected, long actual)
      Asserts that two longs are equal.
      static voidassertEquals​(String message, Object[] expecteds, Object[] actuals)
      已过时。
      use assertArrayEquals
      static voidassertEquals​(String message, Object expected, Object actual)
      Asserts that two objects are equal.
      static voidassertFalse​(boolean condition)
      Asserts that a condition is false.
      static voidassertFalse​(String message, boolean condition)
      Asserts that a condition is false.
      static voidassertNotEquals​(double unexpected, double actual, double delta)
      Asserts that two doubles are not equal to within a positive delta.
      static voidassertNotEquals​(float unexpected, float actual, float delta)
      Asserts that two floats are not equal to within a positive delta.
      static voidassertNotEquals​(long unexpected, long actual)
      Asserts that two longs are not equals.
      static voidassertNotEquals​(Object unexpected, Object actual)
      Asserts that two objects are not equals.
      static voidassertNotEquals​(String message, double unexpected, double actual, double delta)
      Asserts that two doubles are not equal to within a positive delta.
      static voidassertNotEquals​(String message, float unexpected, float actual, float delta)
      Asserts that two floats are not equal to within a positive delta.
      static voidassertNotEquals​(String message, long unexpected, long actual)
      Asserts that two longs are not equals.
      static voidassertNotEquals​(String message, Object unexpected, Object actual)
      Asserts that two objects are not equals.
      static voidassertNotNull​(Object object)
      Asserts that an object isn't null.
      static voidassertNotNull​(String message, Object object)
      Asserts that an object isn't null.
      static voidassertNotSame​(Object unexpected, Object actual)
      Asserts that two objects do not refer to the same object.
      static voidassertNotSame​(String message, Object unexpected, Object actual)
      Asserts that two objects do not refer to the same object.
      static voidassertNull​(Object object)
      Asserts that an object is null.
      static voidassertNull​(String message, Object object)
      Asserts that an object is null.
      static voidassertSame​(Object expected, Object actual)
      Asserts that two objects refer to the same object.
      static voidassertSame​(String message, Object expected, Object actual)
      Asserts that two objects refer to the same object.
      static <T> voidassertThat​(String reason, T actual, Matcher<? super T> matcher)
      Asserts that actual satisfies the condition specified by matcher.
      static <T> voidassertThat​(T actual, Matcher<? super T> matcher)
      Asserts that actual satisfies the condition specified by matcher.
      static voidassertTrue​(boolean condition)
      Asserts that a condition is true.
      static voidassertTrue​(String message, boolean condition)
      Asserts that a condition is true.
      static voidfail()
      Fails a test with no message.
      static voidfail​(String message)
      Fails a test with the given message.
    • 构造器详细资料

      • Assert

        protected Assert()
        Protect constructor since it is a static only class
    • 方法详细资料

      • assertTrue

        public static void assertTrue​(String message,
                                      boolean condition)
        Asserts that a condition is true. If it isn't it throws an AssertionError with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        condition - condition to be checked
      • assertTrue

        public static void assertTrue​(boolean condition)
        Asserts that a condition is true. If it isn't it throws an AssertionError without a message.
        参数:
        condition - condition to be checked
      • assertFalse

        public static void assertFalse​(String message,
                                       boolean condition)
        Asserts that a condition is false. If it isn't it throws an AssertionError with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        condition - condition to be checked
      • assertFalse

        public static void assertFalse​(boolean condition)
        Asserts that a condition is false. If it isn't it throws an AssertionError without a message.
        参数:
        condition - condition to be checked
      • fail

        public static void fail()
        Fails a test with no message.
      • assertEquals

        public static void assertEquals​(String message,
                                        Object expected,
                                        Object actual)
        Asserts that two objects are equal. If they are not, an AssertionError is thrown with the given message. If expected and actual are null, they are considered equal.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expected - expected value
        actual - actual value
      • assertEquals

        public static void assertEquals​(Object expected,
                                        Object actual)
        Asserts that two objects are equal. If they are not, an AssertionError without a message is thrown. If expected and actual are null, they are considered equal.
        参数:
        expected - expected value
        actual - the value to check against expected
      • assertNotEquals

        public static void assertNotEquals​(String message,
                                           Object unexpected,
                                           Object actual)
        Asserts that two objects are not equals. If they are, an AssertionError is thrown with the given message. If unexpected and actual are null, they are considered equal.
        参数:
        message - the identifying message for the AssertionError (null okay)
        unexpected - unexpected value to check
        actual - the value to check against unexpected
      • assertNotEquals

        public static void assertNotEquals​(Object unexpected,
                                           Object actual)
        Asserts that two objects are not equals. If they are, an AssertionError without a message is thrown. If unexpected and actual are null, they are considered equal.
        参数:
        unexpected - unexpected value to check
        actual - the value to check against unexpected
      • assertNotEquals

        public static void assertNotEquals​(String message,
                                           long unexpected,
                                           long actual)
        Asserts that two longs are not equals. If they are, an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        unexpected - unexpected value to check
        actual - the value to check against unexpected
      • assertNotEquals

        public static void assertNotEquals​(long unexpected,
                                           long actual)
        Asserts that two longs are not equals. If they are, an AssertionError without a message is thrown.
        参数:
        unexpected - unexpected value to check
        actual - the value to check against unexpected
      • assertNotEquals

        public static void assertNotEquals​(String message,
                                           double unexpected,
                                           double actual,
                                           double delta)
        Asserts that two doubles are not equal to within a positive delta. If they are, an AssertionError is thrown with the given message. If the unexpected value is infinity then the delta value is ignored. NaNs are considered equal: assertNotEquals(Double.NaN, Double.NaN, *) fails
        参数:
        message - the identifying message for the AssertionError (null okay)
        unexpected - unexpected value
        actual - the value to check against unexpected
        delta - the maximum delta between unexpected and actual for which both numbers are still considered equal.
      • assertNotEquals

        public static void assertNotEquals​(double unexpected,
                                           double actual,
                                           double delta)
        Asserts that two doubles are not equal to within a positive delta. If they are, an AssertionError is thrown. If the unexpected value is infinity then the delta value is ignored.NaNs are considered equal: assertNotEquals(Double.NaN, Double.NaN, *) fails
        参数:
        unexpected - unexpected value
        actual - the value to check against unexpected
        delta - the maximum delta between unexpected and actual for which both numbers are still considered equal.
      • assertNotEquals

        public static void assertNotEquals​(float unexpected,
                                           float actual,
                                           float delta)
        Asserts that two floats are not equal to within a positive delta. If they are, an AssertionError is thrown. If the unexpected value is infinity then the delta value is ignored.NaNs are considered equal: assertNotEquals(Float.NaN, Float.NaN, *) fails
        参数:
        unexpected - unexpected value
        actual - the value to check against unexpected
        delta - the maximum delta between unexpected and actual for which both numbers are still considered equal.
      • assertArrayEquals

        public static void assertArrayEquals​(String message,
                                             Object[] expecteds,
                                             Object[] actuals)
                                      throws ArrayComparisonFailure
        Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null, they are considered equal.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expecteds - Object array or array of arrays (multi-dimensional array) with expected values.
        actuals - Object array or array of arrays (multi-dimensional array) with actual values
        抛出:
        ArrayComparisonFailure
      • assertArrayEquals

        public static void assertArrayEquals​(Object[] expecteds,
                                             Object[] actuals)
        Asserts that two object arrays are equal. If they are not, an AssertionError is thrown. If expected and actual are null, they are considered equal.
        参数:
        expecteds - Object array or array of arrays (multi-dimensional array) with expected values
        actuals - Object array or array of arrays (multi-dimensional array) with actual values
      • assertArrayEquals

        public static void assertArrayEquals​(String message,
                                             boolean[] expecteds,
                                             boolean[] actuals)
                                      throws ArrayComparisonFailure
        Asserts that two boolean arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null, they are considered equal.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expecteds - boolean array with expected values.
        actuals - boolean array with expected values.
        抛出:
        ArrayComparisonFailure
      • assertArrayEquals

        public static void assertArrayEquals​(boolean[] expecteds,
                                             boolean[] actuals)
        Asserts that two boolean arrays are equal. If they are not, an AssertionError is thrown. If expected and actual are null, they are considered equal.
        参数:
        expecteds - boolean array with expected values.
        actuals - boolean array with expected values.
      • assertArrayEquals

        public static void assertArrayEquals​(byte[] expecteds,
                                             byte[] actuals)
        Asserts that two byte arrays are equal. If they are not, an AssertionError is thrown.
        参数:
        expecteds - byte array with expected values.
        actuals - byte array with actual values
      • assertArrayEquals

        public static void assertArrayEquals​(char[] expecteds,
                                             char[] actuals)
        Asserts that two char arrays are equal. If they are not, an AssertionError is thrown.
        参数:
        expecteds - char array with expected values.
        actuals - char array with actual values
      • assertArrayEquals

        public static void assertArrayEquals​(short[] expecteds,
                                             short[] actuals)
        Asserts that two short arrays are equal. If they are not, an AssertionError is thrown.
        参数:
        expecteds - short array with expected values.
        actuals - short array with actual values
      • assertArrayEquals

        public static void assertArrayEquals​(int[] expecteds,
                                             int[] actuals)
        Asserts that two int arrays are equal. If they are not, an AssertionError is thrown.
        参数:
        expecteds - int array with expected values.
        actuals - int array with actual values
      • assertArrayEquals

        public static void assertArrayEquals​(long[] expecteds,
                                             long[] actuals)
        Asserts that two long arrays are equal. If they are not, an AssertionError is thrown.
        参数:
        expecteds - long array with expected values.
        actuals - long array with actual values
      • assertArrayEquals

        public static void assertArrayEquals​(String message,
                                             double[] expecteds,
                                             double[] actuals,
                                             double delta)
                                      throws ArrayComparisonFailure
        Asserts that two double arrays are equal. If they are not, an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expecteds - double array with expected values.
        actuals - double array with actual values
        delta - the maximum delta between expecteds[i] and actuals[i] for which both numbers are still considered equal.
        抛出:
        ArrayComparisonFailure
      • assertArrayEquals

        public static void assertArrayEquals​(double[] expecteds,
                                             double[] actuals,
                                             double delta)
        Asserts that two double arrays are equal. If they are not, an AssertionError is thrown.
        参数:
        expecteds - double array with expected values.
        actuals - double array with actual values
        delta - the maximum delta between expecteds[i] and actuals[i] for which both numbers are still considered equal.
      • assertArrayEquals

        public static void assertArrayEquals​(String message,
                                             float[] expecteds,
                                             float[] actuals,
                                             float delta)
                                      throws ArrayComparisonFailure
        Asserts that two float arrays are equal. If they are not, an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expecteds - float array with expected values.
        actuals - float array with actual values
        delta - the maximum delta between expecteds[i] and actuals[i] for which both numbers are still considered equal.
        抛出:
        ArrayComparisonFailure
      • assertArrayEquals

        public static void assertArrayEquals​(float[] expecteds,
                                             float[] actuals,
                                             float delta)
        Asserts that two float arrays are equal. If they are not, an AssertionError is thrown.
        参数:
        expecteds - float array with expected values.
        actuals - float array with actual values
        delta - the maximum delta between expecteds[i] and actuals[i] for which both numbers are still considered equal.
      • assertEquals

        public static void assertEquals​(String message,
                                        double expected,
                                        double actual,
                                        double delta)
        Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown with the given message. If the expected value is infinity then the delta value is ignored. NaNs are considered equal: assertEquals(Double.NaN, Double.NaN, *) passes
        参数:
        message - the identifying message for the AssertionError (null okay)
        expected - expected value
        actual - the value to check against expected
        delta - the maximum delta between expected and actual for which both numbers are still considered equal.
      • assertEquals

        public static void assertEquals​(String message,
                                        float expected,
                                        float actual,
                                        float delta)
        Asserts that two floats are equal to within a positive delta. If they are not, an AssertionError is thrown with the given message. If the expected value is infinity then the delta value is ignored. NaNs are considered equal: assertEquals(Float.NaN, Float.NaN, *) passes
        参数:
        message - the identifying message for the AssertionError (null okay)
        expected - expected value
        actual - the value to check against expected
        delta - the maximum delta between expected and actual for which both numbers are still considered equal.
      • assertNotEquals

        public static void assertNotEquals​(String message,
                                           float unexpected,
                                           float actual,
                                           float delta)
        Asserts that two floats are not equal to within a positive delta. If they are, an AssertionError is thrown with the given message. If the unexpected value is infinity then the delta value is ignored. NaNs are considered equal: assertNotEquals(Float.NaN, Float.NaN, *) fails
        参数:
        message - the identifying message for the AssertionError (null okay)
        unexpected - unexpected value
        actual - the value to check against unexpected
        delta - the maximum delta between unexpected and actual for which both numbers are still considered equal.
      • assertEquals

        public static void assertEquals​(long expected,
                                        long actual)
        Asserts that two longs are equal. If they are not, an AssertionError is thrown.
        参数:
        expected - expected long value.
        actual - actual long value
      • assertEquals

        public static void assertEquals​(String message,
                                        long expected,
                                        long actual)
        Asserts that two longs are equal. If they are not, an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expected - long expected value.
        actual - long actual value
      • assertEquals

        @Deprecated
        public static void assertEquals​(double expected,
                                        double actual)
        已过时。
        Use assertEquals(double expected, double actual, double delta) instead
      • assertEquals

        @Deprecated
        public static void assertEquals​(String message,
                                        double expected,
                                        double actual)
        已过时。
        Use assertEquals(String message, double expected, double actual, double delta) instead
      • assertEquals

        public static void assertEquals​(double expected,
                                        double actual,
                                        double delta)
        Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta value is ignored.NaNs are considered equal: assertEquals(Double.NaN, Double.NaN, *) passes
        参数:
        expected - expected value
        actual - the value to check against expected
        delta - the maximum delta between expected and actual for which both numbers are still considered equal.
      • assertEquals

        public static void assertEquals​(float expected,
                                        float actual,
                                        float delta)
        Asserts that two floats are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta value is ignored. NaNs are considered equal: assertEquals(Float.NaN, Float.NaN, *) passes
        参数:
        expected - expected value
        actual - the value to check against expected
        delta - the maximum delta between expected and actual for which both numbers are still considered equal.
      • assertNotNull

        public static void assertNotNull​(String message,
                                         Object object)
        Asserts that an object isn't null. If it is an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        object - Object to check or null
      • assertNotNull

        public static void assertNotNull​(Object object)
        Asserts that an object isn't null. If it is an AssertionError is thrown.
        参数:
        object - Object to check or null
      • assertNull

        public static void assertNull​(String message,
                                      Object object)
        Asserts that an object is null. If it is not, an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        object - Object to check or null
      • assertNull

        public static void assertNull​(Object object)
        Asserts that an object is null. If it isn't an AssertionError is thrown.
        参数:
        object - Object to check or null
      • assertSame

        public static void assertSame​(String message,
                                      Object expected,
                                      Object actual)
        Asserts that two objects refer to the same object. If they are not, an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expected - the expected object
        actual - the object to compare to expected
      • assertSame

        public static void assertSame​(Object expected,
                                      Object actual)
        Asserts that two objects refer to the same object. If they are not the same, an AssertionError without a message is thrown.
        参数:
        expected - the expected object
        actual - the object to compare to expected
      • assertNotSame

        public static void assertNotSame​(String message,
                                         Object unexpected,
                                         Object actual)
        Asserts that two objects do not refer to the same object. If they do refer to the same object, an AssertionError is thrown with the given message.
        参数:
        message - the identifying message for the AssertionError (null okay)
        unexpected - the object you don't expect
        actual - the object to compare to unexpected
      • assertNotSame

        public static void assertNotSame​(Object unexpected,
                                         Object actual)
        Asserts that two objects do not refer to the same object. If they do refer to the same object, an AssertionError without a message is thrown.
        参数:
        unexpected - the object you don't expect
        actual - the object to compare to unexpected
      • assertEquals

        @Deprecated
        public static void assertEquals​(String message,
                                        Object[] expecteds,
                                        Object[] actuals)
        已过时。
        use assertArrayEquals
        Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null, they are considered equal.
        参数:
        message - the identifying message for the AssertionError (null okay)
        expecteds - Object array or array of arrays (multi-dimensional array) with expected values.
        actuals - Object array or array of arrays (multi-dimensional array) with actual values
      • assertEquals

        @Deprecated
        public static void assertEquals​(Object[] expecteds,
                                        Object[] actuals)
        已过时。
        use assertArrayEquals
        Asserts that two object arrays are equal. If they are not, an AssertionError is thrown. If expected and actual are null, they are considered equal.
        参数:
        expecteds - Object array or array of arrays (multi-dimensional array) with expected values
        actuals - Object array or array of arrays (multi-dimensional array) with actual values
      • assertThat

        public static <T> void assertThat​(T actual,
                                          Matcher<? super T> matcher)
        Asserts that actual satisfies the condition specified by matcher. If not, an AssertionError is thrown with information about the matcher and failing value. Example:
           assertThat(0, is(1)); // fails:
             // failure message:
             // expected: is <1>
             // got value: <0>
           assertThat(0, is(not(1))) // passes
         
        org.hamcrest.Matcher does not currently document the meaning of its type parameter T. This method assumes that a matcher typed as Matcher<T> can be meaningfully applied only to values that could be assigned to a variable of type T.
        类型参数:
        T - the static type accepted by the matcher (this can flag obvious compile-time problems such as assertThat(1, is("a"))
        参数:
        actual - the computed value being compared
        matcher - an expression, built of Matchers, specifying allowed values
        另请参阅:
        CoreMatchers, MatcherAssert
      • assertThat

        public static <T> void assertThat​(String reason,
                                          T actual,
                                          Matcher<? super T> matcher)
        Asserts that actual satisfies the condition specified by matcher. If not, an AssertionError is thrown with the reason and information about the matcher and failing value. Example:
           assertThat("Help! Integers don't work", 0, is(1)); // fails:
             // failure message:
             // Help! Integers don't work
             // expected: is <1>
             // got value: <0>
           assertThat("Zero is one", 0, is(not(1))) // passes
         
        org.hamcrest.Matcher does not currently document the meaning of its type parameter T. This method assumes that a matcher typed as Matcher<T> can be meaningfully applied only to values that could be assigned to a variable of type T.
        类型参数:
        T - the static type accepted by the matcher (this can flag obvious compile-time problems such as assertThat(1, is("a"))
        参数:
        reason - additional information about the error
        actual - the computed value being compared
        matcher - an expression, built of Matchers, specifying allowed values
        另请参阅:
        CoreMatchers, MatcherAssert