zoukankan      html  css  js  c++  java
  • Assertions

    断言(Assertions)

     Junit
     

    JUnit为所有原始类型和对象以及(原始或对象的)数组提供了重载的断言方法。参数顺序是期望值,然后是实际值。可选地,第一个参数可以是失败时输出的String消息。有一个稍微不同的断言,assertThat它具有可选故障消息,实际值和Matcher对象的参数。请注意,与其他assert方法相比,期望值和实际值是相反的。

    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.not;
    import static org.hamcrest.CoreMatchers.sameInstance;
    import static org.hamcrest.CoreMatchers.startsWith;
    import static org.junit.Assert.assertArrayEquals;
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertFalse;
    import static org.junit.Assert.assertNotNull;
    import static org.junit.Assert.assertNotSame;
    import static org.junit.Assert.assertNull;
    import static org.junit.Assert.assertSame;
    import static org.junit.Assert.assertThat;
    import static org.junit.Assert.assertTrue;
    
    import java.util.Arrays;
    
    import org.hamcrest.core.CombinableMatcher;
    import org.junit.Test;
    
    public class AssertTests {
      @Test
      public void testAssertArrayEquals() {
        byte[] expected = "trial".getBytes();
        byte[] actual = "trial".getBytes();
        assertArrayEquals("failure - byte arrays not same", expected, actual);
      }
    
      @Test
      public void testAssertEquals() {
        
         assertEquals 使用方法:点击进入
     

        assertEquals("failure - long are not equal", 666666, 666666);
        assertEquals("failure - strings are not equal", "Hepeng", "Hepeng");
        assertEquals(3.00,8.00,5.00);

    
    
      

      
    } @Test
    public void testAssertFalse() { assertFalse("failure - should be false", false); } @Test public void testAssertNotNull() { assertNotNull("should not be null", new Object()); } @Test public void testAssertNotSame() {
       //https://www.cnblogs.com/WLCYSYS/p/12767357.html assertNotSame(
    "should not be same Object", new Object(), new Object()); } @Test public void testAssertNull() { assertNull("should be null", null); } @Test public void testAssertSame() { Integer aNumber = Integer.valueOf(888); assertSame("should be same", aNumber, aNumber); } // 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 testAssertTrue() { assertTrue("failure - should be true", true); } }
    tips:
      测试结果:点击进入
  • 相关阅读:
    缩水版遗传算法 学习笔记
    算法导论 二项堆
    Linux系统编程(6)——文件系统
    Linux系统编程(5)——文件与IO之mmap函数
    Linux系统编程(4)——文件与IO之ioctl函数
    Linux系统编程(3)——文件与IO之fcntl函数
    Linux系统编程(2)——文件与IO之系统调用与文件IO操作
    Linux系统编程(1)——文件与I/O之C标准I/O函数与系统调用I/O
    C语言的本质(38)——makefile之变量
    C语言的本质(37)——makefile之隐含规则和模式规则
  • 原文地址:https://www.cnblogs.com/WLCYSYS/p/12767357.html
Copyright © 2011-2022 走看看