springboot 单元测试断言


springboot 单元测试断言

                      

hamcrest 官网:Hamcrest Tutorial

                                 

                     

********************

导入 jar 包

                

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>

                   

                      

********************

相关类与接口            

Assertions

@API(
    status = Status.STABLE,
    since = "5.0"
)
public class Assertions {
    @API(
        status = Status.STABLE,
        since = "5.3"
    )
    protected Assertions() {
    }


*************
fail:抛出异常

    public static <V> V fail() {
    public static <V> V fail(String message) {
    public static <V> V fail(String message, Throwable cause) {
    public static <V> V fail(Throwable cause) {
    public static <V> V fail(Supplier<String> messageSupplier) {


*************
assertTrue

    public static void assertTrue(boolean condition) {
    public static void assertTrue(boolean condition, Supplier<String> messageSupplier) {
    public static void assertTrue(BooleanSupplier booleanSupplier) {
    public static void assertTrue(BooleanSupplier booleanSupplier, String message) {
    public static void assertTrue(boolean condition, String message) {
    public static void assertTrue(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier) {


*************
assertFalse

    public static void assertFalse(boolean condition) {
    public static void assertFalse(boolean condition, String message) {
    public static void assertFalse(boolean condition, Supplier<String> messageSupplier) {
    public static void assertFalse(BooleanSupplier booleanSupplier) {
    public static void assertFalse(BooleanSupplier booleanSupplier, String message) {
    public static void assertFalse(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier) {


*************
assertNull、assertNotNull

    public static void assertNull(Object actual) {
    public static void assertNull(Object actual, String message) {
    public static void assertNull(Object actual, Supplier<String> messageSupplier) {

    public static void assertNotNull(Object actual) {
    public static void assertNotNull(Object actual, String message) {
    public static void assertNotNull(Object actual, Supplier<String> messageSupplier) {


*************
assertEquals

    public static void assertEquals(short expected, short actual) {
    public static void assertEquals(short expected, Short actual) {
    public static void assertEquals(Short expected, short actual) {
    public static void assertEquals(Short expected, Short actual) {
    public static void assertEquals(short expected, short actual, String message) {
    public static void assertEquals(short expected, Short actual, String message) {
    public static void assertEquals(Short expected, short actual, String message) {

    public static void assertEquals(Short expected, Short actual, String message) {
    public static void assertEquals(short expected, short actual, Supplier<String> messageSupplier) {
    public static void assertEquals(short expected, Short actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Short expected, short actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Short expected, Short actual, Supplier<String> messageSupplier) {


    public static void assertEquals(byte expected, byte actual) {
    public static void assertEquals(byte expected, Byte actual) {
    public static void assertEquals(Byte expected, byte actual) {
    public static void assertEquals(Byte expected, Byte actual) {
    public static void assertEquals(byte expected, byte actual, String message) {
    public static void assertEquals(byte expected, Byte actual, String message) {
    public static void assertEquals(Byte expected, byte actual, String message) {
    public static void assertEquals(Byte expected, Byte actual, String message) {
    public static void assertEquals(byte expected, byte actual, Supplier<String> messageSupplier) {
    public static void assertEquals(byte expected, Byte actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Byte expected, byte actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Byte expected, Byte actual, Supplier<String> messageSupplier) {


    public static void assertEquals(int expected, int actual) {
    public static void assertEquals(int expected, Integer actual) {
    public static void assertEquals(Integer expected, int actual) {
    public static void assertEquals(Integer expected, Integer actual) {
    public static void assertEquals(int expected, int actual, String message) {
    public static void assertEquals(int expected, Integer actual, String message) {
    public static void assertEquals(Integer expected, int actual, String message) {
    public static void assertEquals(Integer expected, Integer actual, String message) {
    public static void assertEquals(int expected, int actual, Supplier<String> messageSupplier) {
    public static void assertEquals(int expected, Integer actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Integer expected, int actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Integer expected, Integer actual, Supplier<String> messageSupplier) {


    public static void assertEquals(long expected, long actual) {
    public static void assertEquals(long expected, Long actual) {
    public static void assertEquals(Long expected, long actual) {
    public static void assertEquals(Long expected, Long actual) {
    public static void assertEquals(long expected, long actual, String message) {
    public static void assertEquals(long expected, Long actual, String message) {
    public static void assertEquals(Long expected, long actual, String message) {
    public static void assertEquals(Long expected, Long actual, String message) {
    public static void assertEquals(long expected, long actual, Supplier<String> messageSupplier) {
    public static void assertEquals(long expected, Long actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Long expected, long actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Long expected, Long actual, Supplier<String> messageSupplier) {


    public static void assertEquals(float expected, float actual) {
    public static void assertEquals(float expected, Float actual) {
    public static void assertEquals(Float expected, float actual) {
    public static void assertEquals(Float expected, Float actual) {
    public static void assertEquals(float expected, float actual, String message) {
    public static void assertEquals(float expected, Float actual, String message) {
    public static void assertEquals(Float expected, float actual, String message) {
    public static void assertEquals(Float expected, Float actual, String message) {
    public static void assertEquals(float expected, float actual, Supplier<String> messageSupplier) {
    public static void assertEquals(float expected, Float actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Float expected, float actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Float expected, Float actual, Supplier<String> messageSupplier) {
    public static void assertEquals(float expected, float actual, float delta) {
    public static void assertEquals(float expected, float actual, float delta, String message) {
    public static void assertEquals(float expected, float actual, float delta, Supplier<String> messageSupplier) {


    public static void assertEquals(double expected, double actual) {
    public static void assertEquals(double expected, Double actual) {
    public static void assertEquals(Double expected, double actual) {
    public static void assertEquals(Double expected, Double actual) {
    public static void assertEquals(double expected, double actual, String message) {
    public static void assertEquals(double expected, Double actual, String message) {
    public static void assertEquals(Double expected, double actual, String message) {
    public static void assertEquals(Double expected, Double actual, String message) {
    public static void assertEquals(double expected, double actual, Supplier<String> messageSupplier) {
    public static void assertEquals(double expected, Double actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Double expected, double actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Double expected, Double actual, Supplier<String> messageSupplier) {
    public static void assertEquals(double expected, double actual, double delta) {
    public static void assertEquals(double expected, double actual, double delta, String message) {
    public static void assertEquals(double expected, double actual, double delta, Supplier<String> messageSupplier) {


    public static void assertEquals(char expected, char actual) {
    public static void assertEquals(char expected, Character actual) {
    public static void assertEquals(Character expected, char actual) {
    public static void assertEquals(Character expected, Character actual) {
    public static void assertEquals(char expected, char actual, String message) {
    public static void assertEquals(char expected, Character actual, String message) {
    public static void assertEquals(Character expected, char actual, String message) {
    public static void assertEquals(Character expected, Character actual, String message) {
    public static void assertEquals(char expected, char actual, Supplier<String> messageSupplier) {
    public static void assertEquals(char expected, Character actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Character expected, char actual, Supplier<String> messageSupplier) {
    public static void assertEquals(Character expected, Character actual, Supplier<String> messageSupplier) {


    public static void assertEquals(Object expected, Object actual) {
    public static void assertEquals(Object expected, Object actual, String message) {
    public static void assertEquals(Object expected, Object actual, Supplier<String> messageSupplier) {


*************
assertArrayEquals

    public static void assertArrayEquals(boolean[] expected, boolean[] actual) {
    public static void assertArrayEquals(boolean[] expected, boolean[] actual, String message) {
    public static void assertArrayEquals(boolean[] expected, boolean[] actual, Supplier<String> messageSupplier) {


    public static void assertArrayEquals(char[] expected, char[] actual) {
    public static void assertArrayEquals(char[] expected, char[] actual, String message) {
    public static void assertArrayEquals(char[] expected, char[] actual, Supplier<String> messageSupplier) {

    public static void assertArrayEquals(byte[] expected, byte[] actual) {
    public static void assertArrayEquals(byte[] expected, byte[] actual, String message) {
    public static void assertArrayEquals(byte[] expected, byte[] actual, Supplier<String> messageSupplier) {


    public static void assertArrayEquals(short[] expected, short[] actual) {
    public static void assertArrayEquals(short[] expected, short[] actual, String message) {
    public static void assertArrayEquals(short[] expected, short[] actual, Supplier<String> messageSupplier) {


    public static void assertArrayEquals(int[] expected, int[] actual) {
    public static void assertArrayEquals(int[] expected, int[] actual, String message) {
    public static void assertArrayEquals(int[] expected, int[] actual, Supplier<String> messageSupplier) {


    public static void assertArrayEquals(long[] expected, long[] actual) {
    public static void assertArrayEquals(long[] expected, long[] actual, String message) {
    public static void assertArrayEquals(long[] expected, long[] actual, Supplier<String> messageSupplier) {


    public static void assertArrayEquals(float[] expected, float[] actual) {
    public static void assertArrayEquals(float[] expected, float[] actual, String message) {
    public static void assertArrayEquals(float[] expected, float[] actual, Supplier<String> messageSupplier) {
    public static void assertArrayEquals(float[] expected, float[] actual, float delta) {
    public static void assertArrayEquals(float[] expected, float[] actual, float delta, String message) {
    public static void assertArrayEquals(float[] expected, float[] actual, float delta, Supplier<String> messageSupplier) {


    public static void assertArrayEquals(double[] expected, double[] actual) {
    public static void assertArrayEquals(double[] expected, double[] actual, String message) {
    public static void assertArrayEquals(double[] expected, double[] actual, Supplier<String> messageSupplier) {
    public static void assertArrayEquals(double[] expected, double[] actual, double delta) {
    public static void assertArrayEquals(double[] expected, double[] actual, double delta, String message) {
    public static void assertArrayEquals(double[] expected, double[] actual, double delta, Supplier<String> messageSupplier) {


    public static void assertArrayEquals(Object[] expected, Object[] actual) {
    public static void assertArrayEquals(Object[] expected, Object[] actual, String message) {
    public static void assertArrayEquals(Object[] expected, Object[] actual, Supplier<String> messageSupplier) {


*************
assertIterableEquals

    public static void assertIterableEquals(Iterable<?> expected, Iterable<?> actual) {
    public static void assertIterableEquals(Iterable<?> expected, Iterable<?> actual, String message) {
    public static void assertIterableEquals(Iterable<?> expected, Iterable<?> actual, Supplier<String> messageSupplier) {


*************
assertLinesMatch

    public static void assertLinesMatch(List<String> expectedLines, List<String> actualLines) {
    public static void assertLinesMatch(List<String> expectedLines, List<String> actualLines, String message) {
    public static void assertLinesMatch(List<String> expectedLines, List<String> actualLines, Supplier<String> messageSupplier) {


    public static void assertLinesMatch(Stream<String> expectedLines, Stream<String> actualLines) {
    public static void assertLinesMatch(Stream<String> expectedLines, Stream<String> actualLines, String message) {
    public static void assertLinesMatch(Stream<String> expectedLines, Stream<String> actualLines, Supplier<String> messageSupplier) {


*************
assertNotEquals

    public static void assertNotEquals(byte unexpected, byte actual) {
    public static void assertNotEquals(byte unexpected, Byte actual) {
    public static void assertNotEquals(Byte unexpected, byte actual) {
    public static void assertNotEquals(Byte unexpected, Byte actual) {
    public static void assertNotEquals(byte unexpected, byte actual, String message) {
    public static void assertNotEquals(byte unexpected, Byte actual, String message) {
    public static void assertNotEquals(Byte unexpected, byte actual, String message) {
    public static void assertNotEquals(Byte unexpected, Byte actual, String message) {
    public static void assertNotEquals(byte unexpected, byte actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(byte unexpected, Byte actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Byte unexpected, byte actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Byte unexpected, Byte actual, Supplier<String> messageSupplier) {


    public static void assertNotEquals(short unexpected, short actual) {
    public static void assertNotEquals(short unexpected, Short actual) {
    public static void assertNotEquals(Short unexpected, short actual) {
    public static void assertNotEquals(Short unexpected, Short actual) {
    public static void assertNotEquals(short unexpected, short actual, String message) {
    public static void assertNotEquals(short unexpected, Short actual, String message) {
    public static void assertNotEquals(Short unexpected, short actual, String message) {
    public static void assertNotEquals(Short unexpected, Short actual, String message) {
    public static void assertNotEquals(short unexpected, short actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(short unexpected, Short actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Short unexpected, short actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Short unexpected, Short actual, Supplier<String> messageSupplier) {


    public static void assertNotEquals(int unexpected, int actual) {
    public static void assertNotEquals(int unexpected, Integer actual) {
    public static void assertNotEquals(Integer unexpected, int actual) {
    public static void assertNotEquals(Integer unexpected, Integer actual) {
    public static void assertNotEquals(int unexpected, int actual, String message) {
    public static void assertNotEquals(int unexpected, Integer actual, String message) {
    public static void assertNotEquals(Integer unexpected, int actual, String message) {
    public static void assertNotEquals(Integer unexpected, Integer actual, String message) {
    public static void assertNotEquals(int unexpected, int actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(int unexpected, Integer actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Integer unexpected, int actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Integer unexpected, Integer actual, Supplier<String> messageSupplier) {


    public static void assertNotEquals(long unexpected, long actual) {
    public static void assertNotEquals(long unexpected, Long actual) {
    public static void assertNotEquals(Long unexpected, long actual) {
    public static void assertNotEquals(Long unexpected, Long actual) {
    public static void assertNotEquals(long unexpected, long actual, String message) {
    public static void assertNotEquals(long unexpected, Long actual, String message) {
    public static void assertNotEquals(Long unexpected, long actual, String message) {
    public static void assertNotEquals(Long unexpected, Long actual, String message) {
    public static void assertNotEquals(long unexpected, long actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(long unexpected, Long actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Long unexpected, long actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Long unexpected, Long actual, Supplier<String> messageSupplier) {


    public static void assertNotEquals(float unexpected, float actual) {
    public static void assertNotEquals(float unexpected, Float actual) {
    public static void assertNotEquals(Float unexpected, float actual) {
    public static void assertNotEquals(Float unexpected, Float actual) {
    public static void assertNotEquals(float unexpected, float actual, String message) {
    public static void assertNotEquals(float unexpected, Float actual, String message) {
    public static void assertNotEquals(Float unexpected, float actual, String message) {
    public static void assertNotEquals(Float unexpected, Float actual, String message) {
    public static void assertNotEquals(float unexpected, float actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(float unexpected, Float actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Float unexpected, float actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Float unexpected, Float actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(float unexpected, float actual, float delta) {
    public static void assertNotEquals(float unexpected, float actual, float delta, String message) {
    public static void assertNotEquals(float unexpected, float actual, float delta, Supplier<String> messageSupplier) {


    public static void assertNotEquals(double unexpected, double actual) {
    public static void assertNotEquals(double unexpected, Double actual) {
    public static void assertNotEquals(Double unexpected, double actual) {
    public static void assertNotEquals(Double unexpected, Double actual) {
    public static void assertNotEquals(double unexpected, double actual, String message) {
    public static void assertNotEquals(double unexpected, Double actual, String message) {
    public static void assertNotEquals(Double unexpected, double actual, String message) {
    public static void assertNotEquals(Double unexpected, Double actual, String message) {
    public static void assertNotEquals(double unexpected, double actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(double unexpected, Double actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Double unexpected, double actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Double unexpected, Double actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(double unexpected, double actual, double delta) {
    public static void assertNotEquals(double unexpected, double actual, double delta, String message) {
    public static void assertNotEquals(double unexpected, double actual, double delta, Supplier<String> messageSupplier) {


    public static void assertNotEquals(char unexpected, char actual) {
    public static void assertNotEquals(char unexpected, Character actual) {
    public static void assertNotEquals(Character unexpected, char actual) {
    public static void assertNotEquals(Character unexpected, Character actual) {
    public static void assertNotEquals(char unexpected, char actual, String message) {
    public static void assertNotEquals(char unexpected, Character actual, String message) {
    public static void assertNotEquals(Character unexpected, char actual, String message) {
    public static void assertNotEquals(Character unexpected, Character actual, String message) {
    public static void assertNotEquals(char unexpected, char actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(char unexpected, Character actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Character unexpected, char actual, Supplier<String> messageSupplier) {
    public static void assertNotEquals(Character unexpected, Character actual, Supplier<String> messageSupplier) {


    public static void assertNotEquals(Object unexpected, Object actual) {
    public static void assertNotEquals(Object unexpected, Object actual, String message) {
    public static void assertNotEquals(Object unexpected, Object actual, Supplier<String> messageSupplier) {


*************
assertSame

    public static void assertSame(Object expected, Object actual) {
    public static void assertSame(Object expected, Object actual, String message) {
    public static void assertSame(Object expected, Object actual, Supplier<String> messageSupplier) {


*************
assertNotSame

    public static void assertNotSame(Object unexpected, Object actual) {
    public static void assertNotSame(Object unexpected, Object actual, String message) {
    public static void assertNotSame(Object unexpected, Object actual, Supplier<String> messageSupplier) {


*************
assertAll

    public static void assertAll(Executable... executables) throws MultipleFailuresError {
    public static void assertAll(String heading, Executable... executables) throws MultipleFailuresError {
    public static void assertAll(Collection<Executable> executables) throws MultipleFailuresError {
    public static void assertAll(String heading, Collection<Executable> executables) throws MultipleFailuresError {
    public static void assertAll(Stream<Executable> executables) throws MultipleFailuresError {
    public static void assertAll(String heading, Stream<Executable> executables) throws MultipleFailuresError {


*************
assertThrowsExactly

    public static <T extends Throwable> T assertThrowsExactly(Class<T> expectedType, Executable executable) {
    public static <T extends Throwable> T assertThrowsExactly(Class<T> expectedType, Executable executable, String message) {
    public static <T extends Throwable> T assertThrowsExactly(Class<T> expectedType, Executable executable, Supplier<String> messageSupplier) {


*************
assertThrows

    public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable) {
    public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable, String message) {
    public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable, Supplier<String> messageSupplier) {


*************
assertDoesNotThrow

    public static void assertDoesNotThrow(Executable executable) {
    public static void assertDoesNotThrow(Executable executable, String message) {
    public static void assertDoesNotThrow(Executable executable, Supplier<String> messageSupplier) {
    public static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier) {
    public static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, String message) {
    public static <T> T assertDoesNotThrow(ThrowingSupplier<T> supplier, Supplier<String> messageSupplier) {


*************
assertTimeout

    public static void assertTimeout(Duration timeout, Executable executable) {
    public static void assertTimeout(Duration timeout, Executable executable, String message) {
    public static void assertTimeout(Duration timeout, Executable executable, Supplier<String> messageSupplier) {
    public static <T> T assertTimeout(Duration timeout, ThrowingSupplier<T> supplier) {
    public static <T> T assertTimeout(Duration timeout, ThrowingSupplier<T> supplier, String message) {
    public static <T> T assertTimeout(Duration timeout, ThrowingSupplier<T> supplier, Supplier<String> messageSupplier) {


*************
assertTimeoutPreemptively

    public static void assertTimeoutPreemptively(Duration timeout, Executable executable) {
    public static void assertTimeoutPreemptively(Duration timeout, Executable executable, String message) {
    public static void assertTimeoutPreemptively(Duration timeout, Executable executable, Supplier<String> messageSupplier) {
    public static <T> T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier<T> supplier) {
    public static <T> T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier<T> supplier, String message) {
    public static <T> T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier<T> supplier, Supplier<String> messageSupplier) {


*************
assertInstanceOf

    public static <T> T assertInstanceOf(Class<T> expectedType, Object actualValue) {
    public static <T> T assertInstanceOf(Class<T> expectedType, Object actualValue, String message) {
    public static <T> T assertInstanceOf(Class<T> expectedType, Object actualValue, Supplier<String> messageSupplier) {
   
   

                                      

                             

MatcherAssert:hamcrest 断言

public class MatcherAssert {
    public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
        assertThat("", actual, matcher);
    }
    
    public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
        if (!matcher.matches(actual)) {
            Description description = new StringDescription();
            description.appendText(reason)
                       .appendText(System.lineSeparator())
                       .appendText("Expected: ")
                       .appendDescriptionOf(matcher)
                       .appendText(System.lineSeparator())
                       .appendText("     but: ");
            matcher.describeMismatch(actual, description);
            
            throw new AssertionError(description.toString());
        }
    }
    
    public static void assertThat(String reason, boolean assertion) {
        if (!assertion) {
            throw new AssertionError(reason);
        }
    }
}

                      

Matcher

public interface Matcher<T> extends SelfDescribing {

    boolean matches(Object actual);
    
    void describeMismatch(Object actual, Description mismatchDescription);

}

                    

Matchers

public class Matchers {


*********
allOf

  public static <T> org.hamcrest.Matcher<T> allOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) {
  public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T>... matchers) {
  public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second) {
  public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) {
  public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) {
  public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) {
  public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) {


*********
anyOf

  public static <T> org.hamcrest.core.AnyOf<T> anyOf(java.lang.Iterable<org.hamcrest.Matcher<? super T>> matchers) {
  public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T>... matchers) {
  public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second) {
  public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) {
  public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) {
  public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) {
  public static <T> org.hamcrest.core.AnyOf<T> anyOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) {


*********
both、either

  public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableBothMatcher<LHS> both(org.hamcrest.Matcher<? super LHS> matcher) {
  public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher<LHS> either(org.hamcrest.Matcher<? super LHS> matcher) {


*********
describedAs

  public static <T> org.hamcrest.Matcher<T> describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values) {


*********
基本操作

  public static <T> org.hamcrest.Matcher<T> in(java.util.Collection<T> collection) {
  public static <T> org.hamcrest.Matcher<T> in(T[] elements) {

  public static <T> org.hamcrest.Matcher<T> oneOf(T... elements) {
  public static <T> org.hamcrest.Matcher<T> any(java.lang.Class<T> type) {

  public static org.hamcrest.Matcher<java.lang.Double> closeTo(double operand, double error) {

  public static org.hamcrest.Matcher<java.lang.Double> notANumber() {

  public static org.hamcrest.Matcher<java.math.BigDecimal> closeTo(java.math.BigDecimal operand, java.math.BigDecimal error) {

  public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> comparesEqualTo(T value) {

  public static <T> org.hamcrest.Matcher<T> equalTo(T operand) {
  public static org.hamcrest.Matcher<java.lang.Object> equalToObject(java.lang.Object operand) {

  public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> greaterThan(T value) {
  public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> greaterThanOrEqualTo(T value) {

  public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> lessThan(T value) {
  public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<T> lessThanOrEqualTo(T value) {


  public static <T> org.hamcrest.Matcher<T> is(org.hamcrest.Matcher<T> matcher) {
  public static <T> org.hamcrest.Matcher<T> is(T value) {

  public static <T> org.hamcrest.Matcher<T> isA(java.lang.Class<?> type) {


  public static org.hamcrest.Matcher<java.lang.Object> anything() {
  public static org.hamcrest.Matcher<java.lang.Object> anything(java.lang.String description) {


  public static <T> org.hamcrest.Matcher<T> instanceOf(java.lang.Class<?> type) {

  public static <T> org.hamcrest.Matcher<T> not(org.hamcrest.Matcher<T> matcher) {
  public static <T> org.hamcrest.Matcher<T> not(T value) {

  public static org.hamcrest.Matcher<java.lang.Object> notNullValue() {
  public static <T> org.hamcrest.Matcher<T> notNullValue(java.lang.Class<T> type) {

  public static org.hamcrest.Matcher<java.lang.Object> nullValue() {
  public static <T> org.hamcrest.Matcher<T> nullValue(java.lang.Class<T> type) {


  public static <T> org.hamcrest.Matcher<java.lang.Class<?>> typeCompatibleWith(java.lang.Class<T> baseType) {

  public static <T> org.hamcrest.Matcher<T> hasToString(org.hamcrest.Matcher<? super java.lang.String> toStringMatcher) {
  public static <T> org.hamcrest.Matcher<T> hasToString(java.lang.String expectedToString) {


*********
字符串操作

  public static Matcher<java.lang.String> emptyString() {
  public static Matcher<java.lang.String> emptyOrNullString() {

  public static Matcher<java.lang.String> blankString() {
  public static Matcher<java.lang.String> blankOrNullString() {

  public static Matcher<CharSequence> hasLength(int length) {

  public static Matcher<java.lang.String> containsString(java.lang.String substring) {
  public static Matcher<java.lang.String> containsStringIgnoringCase(java.lang.String substring) {


  public static Matcher<java.lang.String> startsWith(java.lang.String prefix) {
  public static Matcher<java.lang.String> startsWithIgnoringCase(java.lang.String prefix) {

  public static Matcher<java.lang.String> endsWith(java.lang.String suffix) {
  public static Matcher<java.lang.String> endsWithIgnoringCase(java.lang.String suffix) {

  public static Matcher<String> matchesRegex(Pattern pattern) {
  public static Matcher<String> matchesRegex(String regex) {

  public static Matcher<java.lang.String> matchesPattern(java.util.regex.Pattern pattern) {
  public static Matcher<java.lang.String> matchesPattern(java.lang.String regex) {

  public static Matcher<java.lang.String> equalToIgnoringCase(java.lang.String expectedString) {
  public static Matcher<java.lang.String> equalToCompressingWhiteSpace(java.lang.String expectedString) {

  public static Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings) {
  public static Matcher<java.lang.String> stringContainsInOrder(java.lang.String... substrings) {



*********
bean 操作

  public static <T> org.hamcrest.Matcher<T> sameInstance(T target) {
  public static <T> org.hamcrest.Matcher<T> theInstance(T target) {


  public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyName) {
  public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyName, org.hamcrest.Matcher<?> valueMatcher) {

  public static <B> Matcher<B> samePropertyValuesAs(B expectedBean, String... ignoredProperties) {


*********
map 操作

  public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) {
  public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(int size) {

  public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> anEmptyMap() {

  public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> hasEntry(org.hamcrest.Matcher<? super K> keyMatcher, org.hamcrest.Matcher<? super V> valueMatcher) {
  public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> hasEntry(K key, V value) {

  public static <K> org.hamcrest.Matcher<java.util.Map<? extends K,?>> hasKey(org.hamcrest.Matcher<? super K> keyMatcher) {
  public static <K> org.hamcrest.Matcher<java.util.Map<? extends K,?>> hasKey(K key) {

  public static <V> org.hamcrest.Matcher<java.util.Map<?,? extends V>> hasValue(org.hamcrest.Matcher<? super V> valueMatcher) {
  public static <V> org.hamcrest.Matcher<java.util.Map<?,? extends V>> hasValue(V value) {



*********
数组操作

  public static <E> org.hamcrest.Matcher<E[]> emptyArray() {

  public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(int size) {
  public static <E> org.hamcrest.Matcher<E[]> arrayWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) {

  public static <T> org.hamcrest.collection.IsArray<T> array(org.hamcrest.Matcher<? super T>... elementMatchers) {

  public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(org.hamcrest.Matcher<? super T> elementMatcher) {
  public static <T> org.hamcrest.Matcher<T[]> hasItemInArray(T element) {

  public static <E> org.hamcrest.Matcher<E[]> arrayContaining(E... items) {
  public static <E> org.hamcrest.Matcher<E[]> arrayContaining(org.hamcrest.Matcher<? super E>... itemMatchers) {
  public static <E> org.hamcrest.Matcher<E[]> arrayContaining(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {

  public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcrest.Matcher<? super E>... itemMatchers) {
  public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super E>> itemMatchers) {
  public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(E... items) {



*********
集合操作

  public static <U> org.hamcrest.Matcher<java.lang.Iterable<? extends U>> everyItem(org.hamcrest.Matcher<U> itemMatcher) {

  public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(org.hamcrest.Matcher<? super T> itemMatcher) {
  public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> hasItem(T item) {

  public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? super T>... itemMatchers) {
  public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... items) {


  public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) {
  public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> hasSize(int size) {

  public static <E> org.hamcrest.Matcher<java.util.Collection<? extends E>> empty() {
  public static <E> org.hamcrest.Matcher<java.util.Collection<E>> emptyCollectionOf(java.lang.Class<E> unusedToForceReturnType) {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> emptyIterable() {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> emptyIterableOf(java.lang.Class<E> unusedToForceReturnType) {

  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(E... items) {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E> itemMatcher) {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(org.hamcrest.Matcher<? super E>... itemMatchers) {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {

  public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(org.hamcrest.Matcher<? super T>... itemMatchers) {
  public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(T... items) {
  public static <T> org.hamcrest.Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super T>> itemMatchers) {

  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(E... items) {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(org.hamcrest.Matcher<? super E>... itemMatchers) {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(java.util.List<org.hamcrest.Matcher<? super E>> itemMatchers) {

  public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> iterableWithSize(org.hamcrest.Matcher<? super java.lang.Integer> sizeMatcher) {
  public static <E> org.hamcrest.Matcher<java.lang.Iterable<E>> iterableWithSize(int size) {


*********
event 操作

  public static org.hamcrest.Matcher<java.util.EventObject> eventFrom(java.lang.Class<? extends java.util.EventObject> eventClass, java.lang.Object source) {
  public static org.hamcrest.Matcher<java.util.EventObject> eventFrom(java.lang.Object source) {


*********
hasXPath 操作

  public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, Matcher<java.lang.String> valueMatcher) {
  public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher) {
  public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath) {
  public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext) {

                         

                                

********************

示例

                        

Assertions 断言

package com.example.demo.test;

import lombok.Data;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.hamcrest.Matchers.*;

@Data
class Person{

    private String name;
    private Integer age;
}

public class MyTest {

    public static void testFail(){
        try {
            fail("出错了");
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

    public static void testAssertAll(){
        assertAll("所有任务必须可执行",Arrays.asList(()->{
            System.out.println("hello");
            throw new RuntimeException("出错了");
        }, ()->{
            System.out.println("hello 2");
            throw new RuntimeException("出错了 2");
        }));
    }

    public static void testAssertTrue(){
        assertTrue(true);
        assertTrue(1==1,"1 必须等于 1");
    }

    public static void testAssertNull(){
        assertNull(null);
        assertNull(1,"对象应该为null");
    }

    public static void testAssertEquals(){
        assertEquals(1,1,"1 应该等于 1");
        assertEquals(1.1,1.1,"1.1 应该等于 1.1");
    }

    public static void testAssertArrayEquals(){
        assertArrayEquals(new int[]{1,2,3},new int[]{2,3,4},"数组应该相等");
    }

    public static void testAssertLinesMatch(){
        List<String> expectedList = new ArrayList<>();
        expectedList.add("1");
        expectedList.add("2");
        expectedList.add("3");

        List<String> actualList = new ArrayList<>();
        actualList.add("2");
        actualList.add("3");
        actualList.add("4");

        assertLinesMatch(expectedList,actualList,"字符串链表不相等");
    }

    public static void testAssertSame(){
        Person person = new Person();
        person.setName("瓜田李下");
        person.setAge(20);

        Person person2 = new Person();
        person2.setName("海贼王");
        person2.setAge(20);

        assertSame(person,person2,"person、person2不一样");
    }

    public static void testAssertThrows(){
        Throwable t=assertThrows(Exception.class,()->{
            System.out.println("execute");

            throw new Exception("运行出错");
        },"出错了");
        System.out.println(t.getMessage()+"\n");

        Throwable t2=assertThrows(Exception.class,()->{
            System.out.println("execute");
        },"出错了");
        System.out.println(t2.getMessage());
    }

    public static void testAssertThrowsExactly(){
        Throwable t=assertThrowsExactly(Exception.class,()->{
            System.out.println("execute");

            throw new Exception("运行出错");
        },"出错了");
        System.out.println(t.getMessage()+"\n");

        Throwable t2=assertThrowsExactly(Exception.class,()->{
            System.out.println("execute");

            throw new RuntimeException("运行出错");
        },"出错了");
        System.out.println(t2.getMessage());
    }

    public static void testAssertTimeout(){
                       //执行4s超时后,抛出异常
        assertTimeout(Duration.ofSeconds(2),()->{
            System.out.println("任务开始执行");
            long startTime=System.currentTimeMillis();

            try {
                Thread.sleep(4000);
            }catch (Exception e){
                e.printStackTrace();
            }

            System.out.println("执行间隔:"+(System.currentTimeMillis()-startTime));
        },"任务执行超时");
    }

    public static void testAssertTimeoutPreemptively(){
                       //执行2s超时后,抛出异常
        assertTimeoutPreemptively(Duration.ofSeconds(2),()->{
            System.out.println("任务开始执行");
            long startTime=System.currentTimeMillis();

            try {
                Thread.sleep(4000);
            }catch (Exception e){
                e.printStackTrace();
            }

            System.out.println("执行间隔:"+(System.currentTimeMillis()-startTime));
        },"任务执行超时");
    }

    public static void testInstanceOf(){
        Person person=new Person();
        person.setName("瓜田李下");
        person.setAge(20);

        assertInstanceOf(Person.class,person,"person 应该是类Person的实例对象");
        assertInstanceOf(Object.class,person,"person 应该是类Object的实例对象");
    }

    public static void main(String[] args){
        //testAssertEquals();
        //testAssertArrayEquals();
        //testAssertLinesMatch();
        //testAssertSame();
        //testAssertThrows();
        //testAssertThrowsExactly();
        //testAssertTimeout();
        //testAssertTimeoutPreemptively();
        testInstanceOf();
    }
}

                               

                              

Hamcrest 断言

package com.example.demo.test;

import lombok.Data;

import java.beans.PropertyChangeEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

@Data
class User{

    private String name;
    private Integer age;
}

public class MyTest2 {

    public static void main(String[] args){

        //基本操作
        assertThat("",anything());
        assertThat("",anything("永远匹配"));

        assertThat("cheese", is("cheese"));
        assertThat("cheese", equalTo("cheese"));
        assertThat("cheese", is(equalTo("cheese")));

        assertThat("cheese", not("smelly"));
        assertThat("cheese", not(equalTo("smelly")));
        assertThat("cheese", is(not(equalTo("smelly"))));

        assertThat("cheese", nullValue());
        assertThat("cheese", is(nullValue()));
        assertThat("cheese", nullValue(String.class));

        assertThat("cheese", notNullValue());
        assertThat("cheese", is(notNullValue()));
        assertThat("cheese", is(notNullValue(String.class)));

        assertThat("foo", oneOf("bar", "foo"));

        assertThat("myValue", allOf(startsWith("my"), containsString("Val")));
        assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")));

        assertThat("fab", both(containsString("a")).and(containsString("b")));
        assertThat("fan", either(containsString("a")).or(containsString("b")));

        assertThat(Double.NaN, is(notANumber()));
        assertThat(Integer.class, typeCompatibleWith(Number.class));

        assertThat(1, lessThan(2));
        assertThat(1, lessThanOrEqualTo(1));
        assertThat(1, comparesEqualTo(1));
        assertThat(2, greaterThan(1));
        assertThat(1, greaterThanOrEqualTo(1));

        assertThat(1.03, is(closeTo(1.0, 0.03)));
        assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))));

        BigDecimal bigDecimal=new BigDecimal("11111111");
        assertThat(new BigDecimal("11111111"),describedAs("a big decimal equal to %0", equalTo(bigDecimal),bigDecimal.toPlainString()));


        //string 操作
        assertThat("", is(emptyString()));
        assertThat(null, is(emptyOrNullString()));
        assertThat(null, is(blankOrNullString()));
        assertThat("  ", is(blankString()));

        assertThat("text", hasLength(4));
        assertThat(true, hasToString("TRUE"));
        assertThat(true, hasToString(equalTo("TRUE")));

        assertThat("myStringOfNote", containsString("ring"));
        assertThat("myStringOfNote", containsStringIgnoringCase("Ring"));

        assertThat("myfoobarbaz", stringContainsInOrder("bar", "foo"));
        assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")));

        assertThat("myStringOfNote", startsWith("my"));
        assertThat("myStringOfNote", startsWithIgnoringCase("My"));
        assertThat("myStringOfNote", endsWith("Note"));
        assertThat("myStringOfNote", endsWithIgnoringCase("note"));

        assertThat("abc", matchesRegex("ˆ[a-z]+$"));
        assertThat("abc", matchesRegex(Pattern.compile("ˆ[a-z]$")));

        assertThat("abc", matchesPattern("ˆ[a-z]+$"));
        assertThat("abc", matchesPattern(Pattern.compile("ˆ[a-z]$")));

        assertThat("Foo", equalToIgnoringCase("FOO"));
        assertThat("   my\tfoo  bar ", equalToCompressingWhiteSpace(" my  foo bar"));



        //bean 操作
        User user=new User();
        user.setName("瓜田李下");
        user.setAge(20);
        assertThat(user,any(User.class));
        assertThat(user,isA(User.class));
        assertThat(user,is(instanceOf(User.class)));

        assertThat(user,sameInstance(user));
        assertThat(user,theInstance(user));

        assertThat("foo", equalTo("foo"));
        assertThat("foo",equalToObject("foo"));
        assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"}));

        assertThat(user,hasProperty("name"));
        assertThat(user,hasProperty("name",equalTo("瓜田李下")));

        assertThat(user, samePropertyValuesAs(new User()));
        assertThat(user,samePropertyValuesAs(new User(),"name","age"));


        //map 操作
        Map<String,Object> map=new HashMap<>();
        map.put("name","瓜田李下");
        map.put("age",20);

        assertThat(map, is(aMapWithSize(2)));
        assertThat(map, is(aMapWithSize(equalTo(2))));

        assertThat(map, is(anEmptyMap()));

        assertThat(map, hasKey("bar"));
        assertThat(map, hasKey(equalTo("bar")));

        assertThat(map, hasValue("foo"));
        assertThat(map, hasValue(equalTo("foo")));

        assertThat(map, hasEntry("bar", "foo"));
        assertThat(map, hasEntry(equalTo("bar"), equalTo("foo")));


        //array 操作
        assertThat(new String[0], emptyArray());

        assertThat(new String[]{"foo", "bar"}, arrayWithSize(2));
        assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)));

        assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))));
        assertThat(new String[] {"foo", "bar"}, hasItemInArray("ba"));
        assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")));
        assertThat(new String[]{"foo", "bar"}, arrayContaining("foo", "bar"));
        assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")));
        assertThat(new String[]{"foo", "bar"}, arrayContaining(Arrays.asList(equalTo("foo"), equalTo("bar"))));

        assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder("bar", "foo"));
        assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")));
        assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))));




        //collection 操作
        assertThat(new ArrayList<String>(), is(empty()));
        assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)));

        assertThat(new ArrayList<String>(), is(emptyIterable()));
        assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)));

        assertThat(Arrays.asList("foo", "bar"), hasSize(2));
        assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)));

        assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"));
        assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo")));
        assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")));
        assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))));

        assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"));
        assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(equalTo("bar"), equalTo("foo")));
        assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))));

        assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder("b", "d"));
        assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder(equalTo("b"), equalTo("d")));
        assertThat(Arrays.asList("a", "b", "c", "d", "e"), contains(Arrays.asList(equalTo("b"), equalTo("d"))));

        assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2));
        assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)));

        assertThat("foo", is(in(Arrays.asList("bar", "foo"))));
        assertThat("foo", is(in(new String[]{"bar", "foo"})));

        assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")));
        assertThat(Arrays.asList("foo", "bar"), hasItem("bar"));
        assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")));
        assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"));
        assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")));
    }
}

                           

                                 

猜你喜欢

转载自blog.csdn.net/weixin_43931625/article/details/120635158