9. assertThat:equalTo、not、is、either-or、both-and、anyOf、自定义异常日志

package lesson9;

import org.junit.Test;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class AssertMatcherTest {

    @Test
    public void test() {
        int i = 10;
        assertThat(i, equalTo(10));
        assertThat(i, not(equalTo(20)));
        assertThat(i, is(10));
        assertThat(i, is(not(20)));
        assertThat(i, is(not(equalTo(20))));

        double f = 5.28;
        assertThat(f, either(is(5.28)).or(equalTo(5.55)).or(not(6.66)));
        assertThat(f, anyOf(is(5.28), equalTo(5.55), not(6.66)));
        assertThat(f, both(equalTo(5.28)).and(not(equalTo(5.55))).and(is(not(6.66))));

        assertThat(Stream.of(1, 2, 3).anyMatch(integer -> integer >= 3), equalTo(true));
        assertThat(Stream.of(1, 2, 3).allMatch(integer -> integer > 0), equalTo(true));
    }

    @Test
    public void test2() {
        double f = 5.28;
        /**
         * 此testcase会失败,并抛出自定义信息
         */
        assertThat("match failure...", f, is(5.55));
    }
}

 

posted @ 2020-11-23 16:29  氯雷他定  阅读(336)  评论(0编辑  收藏  举报