springboot 单元测试断言


springboot 单元测试断言

                      

hamcrest 官网:Hamcrest Tutorial

                                 

                     

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

导入 jar 包

                

        
            org.junit.jupiter
            junit-jupiter-api
            5.8.1
            test
        

        
            org.hamcrest
            hamcrest
            2.2
            test
        

                   

                      

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

相关类与接口            

Assertions

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


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

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


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

    public static void assertTrue(boolean condition) {
    public static void assertTrue(boolean condition, Supplier 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 messageSupplier) {


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

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


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

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

    public static void assertNotNull(Object actual) {
    public static void assertNotNull(Object actual, String message) {
    public static void assertNotNull(Object actual, Supplier 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 messageSupplier) {
    public static void assertEquals(short expected, Short actual, Supplier messageSupplier) {
    public static void assertEquals(Short expected, short actual, Supplier messageSupplier) {
    public static void assertEquals(Short expected, Short actual, Supplier 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 messageSupplier) {
    public static void assertEquals(byte expected, Byte actual, Supplier messageSupplier) {
    public static void assertEquals(Byte expected, byte actual, Supplier messageSupplier) {
    public static void assertEquals(Byte expected, Byte actual, Supplier 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 messageSupplier) {
    public static void assertEquals(int expected, Integer actual, Supplier messageSupplier) {
    public static void assertEquals(Integer expected, int actual, Supplier messageSupplier) {
    public static void assertEquals(Integer expected, Integer actual, Supplier 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 messageSupplier) {
    public static void assertEquals(long expected, Long actual, Supplier messageSupplier) {
    public static void assertEquals(Long expected, long actual, Supplier messageSupplier) {
    public static void assertEquals(Long expected, Long actual, Supplier 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 messageSupplier) {
    public static void assertEquals(float expected, Float actual, Supplier messageSupplier) {
    public static void assertEquals(Float expected, float actual, Supplier messageSupplier) {
    public static void assertEquals(Float expected, Float actual, Supplier 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 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 messageSupplier) {
    public static void assertEquals(double expected, Double actual, Supplier messageSupplier) {
    public static void assertEquals(Double expected, double actual, Supplier messageSupplier) {
    public static void assertEquals(Double expected, Double actual, Supplier 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 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 messageSupplier) {
    public static void assertEquals(char expected, Character actual, Supplier messageSupplier) {
    public static void assertEquals(Character expected, char actual, Supplier messageSupplier) {
    public static void assertEquals(Character expected, Character actual, Supplier 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 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 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 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 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 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 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 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 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 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 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 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 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 messageSupplier) {


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

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


    public static void assertLinesMatch(Stream expectedLines, Stream actualLines) {
    public static void assertLinesMatch(Stream expectedLines, Stream actualLines, String message) {
    public static void assertLinesMatch(Stream expectedLines, Stream actualLines, Supplier 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 messageSupplier) {
    public static void assertNotEquals(byte unexpected, Byte actual, Supplier messageSupplier) {
    public static void assertNotEquals(Byte unexpected, byte actual, Supplier messageSupplier) {
    public static void assertNotEquals(Byte unexpected, Byte actual, Supplier 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 messageSupplier) {
    public static void assertNotEquals(short unexpected, Short actual, Supplier messageSupplier) {
    public static void assertNotEquals(Short unexpected, short actual, Supplier messageSupplier) {
    public static void assertNotEquals(Short unexpected, Short actual, Supplier 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 messageSupplier) {
    public static void assertNotEquals(int unexpected, Integer actual, Supplier messageSupplier) {
    public static void assertNotEquals(Integer unexpected, int actual, Supplier messageSupplier) {
    public static void assertNotEquals(Integer unexpected, Integer actual, Supplier 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 messageSupplier) {
    public static void assertNotEquals(long unexpected, Long actual, Supplier messageSupplier) {
    public static void assertNotEquals(Long unexpected, long actual, Supplier messageSupplier) {
    public static void assertNotEquals(Long unexpected, Long actual, Supplier 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 messageSupplier) {
    public static void assertNotEquals(float unexpected, Float actual, Supplier messageSupplier) {
    public static void assertNotEquals(Float unexpected, float actual, Supplier messageSupplier) {
    public static void assertNotEquals(Float unexpected, Float actual, Supplier 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 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 messageSupplier) {
    public static void assertNotEquals(double unexpected, Double actual, Supplier messageSupplier) {
    public static void assertNotEquals(Double unexpected, double actual, Supplier messageSupplier) {
    public static void assertNotEquals(Double unexpected, Double actual, Supplier 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 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 messageSupplier) {
    public static void assertNotEquals(char unexpected, Character actual, Supplier messageSupplier) {
    public static void assertNotEquals(Character unexpected, char actual, Supplier messageSupplier) {
    public static void assertNotEquals(Character unexpected, Character actual, Supplier 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 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 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 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 executables) throws MultipleFailuresError {
    public static void assertAll(String heading, Collection executables) throws MultipleFailuresError {
    public static void assertAll(Stream executables) throws MultipleFailuresError {
    public static void assertAll(String heading, Stream executables) throws MultipleFailuresError {


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

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


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

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


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

    public static void assertDoesNotThrow(Executable executable) {
    public static void assertDoesNotThrow(Executable executable, String message) {
    public static void assertDoesNotThrow(Executable executable, Supplier messageSupplier) {
    public static  T assertDoesNotThrow(ThrowingSupplier supplier) {
    public static  T assertDoesNotThrow(ThrowingSupplier supplier, String message) {
    public static  T assertDoesNotThrow(ThrowingSupplier supplier, Supplier 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 messageSupplier) {
    public static  T assertTimeout(Duration timeout, ThrowingSupplier supplier) {
    public static  T assertTimeout(Duration timeout, ThrowingSupplier supplier, String message) {
    public static  T assertTimeout(Duration timeout, ThrowingSupplier supplier, Supplier 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 messageSupplier) {
    public static  T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier supplier) {
    public static  T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier supplier, String message) {
    public static  T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier supplier, Supplier messageSupplier) {


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

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

                                      

                             

MatcherAssert:hamcrest 断言

public class MatcherAssert {
    public static  void assertThat(T actual, Matcher matcher) {
        assertThat("", actual, matcher);
    }
    
    public static  void assertThat(String reason, T actual, Matcher 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 extends SelfDescribing {

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

}

                    

Matchers

public class Matchers {


*********
allOf

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


*********
anyOf

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


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

  public static  org.hamcrest.core.CombinableMatcher.CombinableBothMatcher both(org.hamcrest.Matcher matcher) {
  public static  org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher either(org.hamcrest.Matcher matcher) {


*********
describedAs

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


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

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

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

  public static org.hamcrest.Matcher closeTo(double operand, double error) {

  public static org.hamcrest.Matcher notANumber() {

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

  public static > org.hamcrest.Matcher comparesEqualTo(T value) {

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

  public static > org.hamcrest.Matcher greaterThan(T value) {
  public static > org.hamcrest.Matcher greaterThanOrEqualTo(T value) {

  public static > org.hamcrest.Matcher lessThan(T value) {
  public static > org.hamcrest.Matcher lessThanOrEqualTo(T value) {


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

  public static  org.hamcrest.Matcher isA(java.lang.Class type) {


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


  public static  org.hamcrest.Matcher instanceOf(java.lang.Class type) {

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

  public static org.hamcrest.Matcher notNullValue() {
  public static  org.hamcrest.Matcher notNullValue(java.lang.Class type) {

  public static org.hamcrest.Matcher nullValue() {
  public static  org.hamcrest.Matcher nullValue(java.lang.Class type) {


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

  public static  org.hamcrest.Matcher hasToString(org.hamcrest.Matcher toStringMatcher) {
  public static  org.hamcrest.Matcher hasToString(java.lang.String expectedToString) {


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

  public static Matcher emptyString() {
  public static Matcher emptyOrNullString() {

  public static Matcher blankString() {
  public static Matcher blankOrNullString() {

  public static Matcher hasLength(int length) {

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


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

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

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

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

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

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



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

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


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

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


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

  public static  org.hamcrest.Matcher> aMapWithSize(org.hamcrest.Matcher sizeMatcher) {
  public static  org.hamcrest.Matcher> aMapWithSize(int size) {

  public static  org.hamcrest.Matcher> anEmptyMap() {

  public static  org.hamcrest.Matcher> hasEntry(org.hamcrest.Matcher keyMatcher, org.hamcrest.Matcher valueMatcher) {
  public static  org.hamcrest.Matcher> hasEntry(K key, V value) {

  public static  org.hamcrest.Matcher> hasKey(org.hamcrest.Matcher keyMatcher) {
  public static  org.hamcrest.Matcher> hasKey(K key) {

  public static  org.hamcrest.Matcher> hasValue(org.hamcrest.Matcher valueMatcher) {
  public static  org.hamcrest.Matcher> hasValue(V value) {



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

  public static  org.hamcrest.Matcher emptyArray() {

  public static  org.hamcrest.Matcher arrayWithSize(int size) {
  public static  org.hamcrest.Matcher arrayWithSize(org.hamcrest.Matcher sizeMatcher) {

  public static  org.hamcrest.collection.IsArray array(org.hamcrest.Matcher... elementMatchers) {

  public static  org.hamcrest.Matcher hasItemInArray(org.hamcrest.Matcher elementMatcher) {
  public static  org.hamcrest.Matcher hasItemInArray(T element) {

  public static  org.hamcrest.Matcher arrayContaining(E... items) {
  public static  org.hamcrest.Matcher arrayContaining(org.hamcrest.Matcher... itemMatchers) {
  public static  org.hamcrest.Matcher arrayContaining(java.util.List> itemMatchers) {

  public static  org.hamcrest.Matcher arrayContainingInAnyOrder(org.hamcrest.Matcher... itemMatchers) {
  public static  org.hamcrest.Matcher arrayContainingInAnyOrder(java.util.Collection> itemMatchers) {
  public static  org.hamcrest.Matcher arrayContainingInAnyOrder(E... items) {



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

  public static  org.hamcrest.Matcher> everyItem(org.hamcrest.Matcher itemMatcher) {

  public static  org.hamcrest.Matcher> hasItem(org.hamcrest.Matcher itemMatcher) {
  public static  org.hamcrest.Matcher> hasItem(T item) {

  public static  org.hamcrest.Matcher> hasItems(org.hamcrest.Matcher... itemMatchers) {
  public static  org.hamcrest.Matcher> hasItems(T... items) {


  public static  org.hamcrest.Matcher> hasSize(org.hamcrest.Matcher sizeMatcher) {
  public static  org.hamcrest.Matcher> hasSize(int size) {

  public static  org.hamcrest.Matcher> empty() {
  public static  org.hamcrest.Matcher> emptyCollectionOf(java.lang.Class unusedToForceReturnType) {
  public static  org.hamcrest.Matcher> emptyIterable() {
  public static  org.hamcrest.Matcher> emptyIterableOf(java.lang.Class unusedToForceReturnType) {

  public static  org.hamcrest.Matcher> contains(E... items) {
  public static  org.hamcrest.Matcher> contains(org.hamcrest.Matcher itemMatcher) {
  public static  org.hamcrest.Matcher> contains(org.hamcrest.Matcher... itemMatchers) {
  public static  org.hamcrest.Matcher> contains(java.util.List> itemMatchers) {

  public static  org.hamcrest.Matcher> containsInAnyOrder(org.hamcrest.Matcher... itemMatchers) {
  public static  org.hamcrest.Matcher> containsInAnyOrder(T... items) {
  public static  org.hamcrest.Matcher> containsInAnyOrder(java.util.Collection> itemMatchers) {

  public static  org.hamcrest.Matcher> containsInRelativeOrder(E... items) {
  public static  org.hamcrest.Matcher> containsInRelativeOrder(org.hamcrest.Matcher... itemMatchers) {
  public static  org.hamcrest.Matcher> containsInRelativeOrder(java.util.List> itemMatchers) {

  public static  org.hamcrest.Matcher> iterableWithSize(org.hamcrest.Matcher sizeMatcher) {
  public static  org.hamcrest.Matcher> iterableWithSize(int size) {


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

  public static org.hamcrest.Matcher eventFrom(java.lang.Class eventClass, java.lang.Object source) {
  public static org.hamcrest.Matcher eventFrom(java.lang.Object source) {


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

  public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, Matcher valueMatcher) {
  public static org.hamcrest.Matcher hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher valueMatcher) {
  public static org.hamcrest.Matcher hasXPath(java.lang.String xPath) {
  public static org.hamcrest.Matcher 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 expectedList = new ArrayList<>();
        expectedList.add("1");
        expectedList.add("2");
        expectedList.add("3");

        List 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 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(), is(empty()));
        assertThat(new ArrayList(), is(emptyCollectionOf(String.class)));

        assertThat(new ArrayList(), is(emptyIterable()));
        assertThat(new ArrayList(), 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")));
    }
}

                           

                                 

你可能感兴趣的:(springboot,单元测试)