美文网首页
AssertJ 断言框架用法总结

AssertJ 断言框架用法总结

作者: 林万程 | 来源:发表于2020-02-17 17:37 被阅读0次

    [TOC]

    AssertJ 断言

    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <!-- use 2.9.1 for Java 7 projects -->
      <version>3.15.0</version>
      <scope>test</scope>
    </dependency>
    
    import static org.assertj.core.api.Assertions.*;
    
    // 假设条件,满足则继续执行
    assumeThat(frodo.getRace()).isNotEqualTo(ORC);
    // 设置错误消息必须在调用断言之前完成
    assertThat(frodo.getAge())
            .as("check %s's age", frodo.getName())
            .isNotNull().isNotZero().isGreaterThanOrEqualTo(80).isLessThan(200)
            .isPositive().isNotNegative()
            .isIn(Arrays.asList(100, 200));
    // Output:
    // [check Frodo's age] expected:<100> but was:<33>
    
    

    时间

    assertThat(dateTime)
            .isBefore("2004-12-13T21:39:45.618-08:00")
            .isAfterYear(2013)
            .isBetween("2018-01-01", "2018-08-31")
            .isInSameHourAs(new Date())
            .isToday();
    

    文件和文件内容

    assertThat(xFile).exists().isFile().isRelative();
    assertThat(contentOf(xFile)).startsWith("The Truth").contains("Is Out").endsWith("There");
    

    字段

    assertThat(sherlock)
            .usingRecursiveComparison() // 只比较字段
            .withStrictTypeChecking() // 同时比较类型(可以是子类形)
            .ignoringFields("name", "home.address.street")
            .ignoringFieldsMatchingRegexes(".*me")
            .ignoringActualNullFields()
            .ignoringFieldsOfTypes(double.class, Address.class)
            .isEqualToIgnoringCase(sherlockClone);
    

    软断言

    try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
        softly.assertThat("George Martin").as("great authors").isEqualTo("JRR Tolkien");  
        softly.assertThat(42).as("response to Everything").isGreaterThan(100); 
    }
    

    AssertJ-DB 断言数据库

    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-db</artifactId>
      <version>1.3.0</version>
      <scope>test</scope>
    </dependency>
    
    import static org.assertj.db.api.Assertions.assertThat;
    
    Source source = new Source("jdbc:h2:mem:test", "sa", "");
    Table table = new Table(source, "members");
    Request request = new Request(dataSource, 
            "select name, firstname from members " +
            "where name like ? and firstname like ?;",
            "%e%", "%Paul%");
    // Check column "name" values
    assertThat(tableOrRequest).column("name")
            .value().isEqualTo("Clayton")
            .value().isEqualTo("Mullen");
    // Check row at index 1 (the second row) values
    assertThat(tableOrRequest).row(1)
            .value().isEqualTo(2)
            .value().isEqualTo("Evans")
            .value().isEqualTo(DateValue.of(1961, 8, 8))
            .value().isEqualTo(1.77);
    

    附:谷歌 Truth 用法

    <dependency>
      <groupId>com.google.truth</groupId>
      <artifactId>truth</artifactId>
      <version>1.0.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>com.google.truth.extensions</groupId>
      <artifactId>truth-java8-extension</artifactId>
      <version>1.0.1</version>
      <scope>test</scope>
    </dependency>
    
    import static com.google.common.truth.Truth.assertThat;
    import static com.google.common.truth.Truth.assertWithMessage;
    import static com.google.common.truth.Truth8.assertThat;
    
    assertThat(string).startsWith("awe");
    assertWithMessage("Without me, it's just aweso").that(string).contains("me");
    

    相关文章

      网友评论

          本文标题:AssertJ 断言框架用法总结

          本文链接:https://www.haomeiwen.com/subject/anqifhtx.html