zoukankan      html  css  js  c++  java
  • 【JUnit】JUnit Hamcrest 断言

    环境

    • JDK 6
    • JUnit 4.13
    • Spring Tool Suite 4.6.2
    • Maven 3.6.3

    断言

    在程序设计中,断言(assertion)是一种放在程序中的一阶逻辑(如一个结果为真或是假的逻辑判断式),目的是为了标示与验证程序开发者预期的结果-当程序运行到断言的位置时,对应的断言应该为真。若断言不为真时,程序会中止运行,并给出错误消息。 --来源于维基百科

    Hamcrest 断言示例

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>jiangbo.java.junit</groupId>
        <artifactId>13-java-junit-hamcrest</artifactId>
        <version>1.0.0</version>
        <description>JUnit Hamcrest 断言示例</description>
    
        <properties>
            <maven.compiler.source>1.6</maven.compiler.source>
            <maven.compiler.target>1.6</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
    </project>
    

    CaculatorTest

    示例来源于官网

    package jiangbo.java.junit;
    
    import static org.hamcrest.CoreMatchers.allOf;
    import static org.hamcrest.CoreMatchers.anyOf;
    import static org.hamcrest.CoreMatchers.both;
    import static org.hamcrest.CoreMatchers.containsString;
    import static org.hamcrest.CoreMatchers.equalTo;
    import static org.hamcrest.CoreMatchers.everyItem;
    import static org.hamcrest.CoreMatchers.hasItems;
    import static org.hamcrest.CoreMatchers.is;
    import static org.hamcrest.CoreMatchers.not;
    import static org.hamcrest.CoreMatchers.sameInstance;
    import static org.hamcrest.CoreMatchers.startsWith;
    import static org.hamcrest.MatcherAssert.assertThat;
    
    import java.util.Arrays;
    
    import org.hamcrest.core.CombinableMatcher;
    import org.junit.Test;
    
    public class CaculatorTest {
    
        // JUnit Matchers assertThat
        @Test
        public void testAssertThatBothContainsString() {
    
            assertThat("albumen", both(containsString("a")).and(containsString("b")));
        }
    
        @Test
        public void testAssertThatHasItems() {
    
            assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
        }
    
        @Test
        public void testAssertThatEveryItemContainsString() {
    
            assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
        }
    
        // Core Hamcrest Matchers with assertThat
        @Test
        public void testAssertThatHamcrestCoreMatchers() {
    
            assertThat("good", allOf(equalTo("good"), startsWith("good")));
            assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
            assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
            assertThat(7, not(CombinableMatcher.<Integer>either(equalTo(3)).or(equalTo(4))));
            assertThat(new Object(), not(sameInstance(new Object())));
        }
    
        @Test
        public void testHamcrest() throws Exception {
    
            assertThat(3, is(3));
            assertThat(3, is(not(4)));
        }
    }
    
  • 相关阅读:
    Python中__new__()方法的使用和实例化
    python __str__ , __repr__区别
    python中%r和%s的区别
    面向对象进阶
    getattr(sys.modules[__name__], func_name)
    os.path.dirname(__file__)使用
    bound和unbound方法,类的绑定和非绑定是什么
    基于python中staticmethod和classmethod的区别(详解)
    python基础----特性(property)、静态方法(staticmethod)、类方法(classmethod)、__str__的用法
    python @classmethod 的使用场合
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/13166559.html
Copyright © 2011-2022 走看看