• Junit 单元测试


    1.核心——注解

    注解

    执行顺序

    一个测试类单元测试的执行顺序为:

    @BeforeClass –> @Before –> @Test –> @After –> @AfterClass

    每一个测试方法的调用顺序为:

    @Before –> @Test –> @After

    2.断言核心方法

    核心断言方法

    package test;  
      
    import static org.hamcrest.CoreMatchers.*;  
    import static org.junit.Assert.*;  
      
    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();  
            org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);  
          }  
      
          @Test  
          public void testAssertEquals() {  
            org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);  
          }  
      
          @Test  
          public void testAssertFalse() {  
            org.junit.Assert.assertFalse("failure - should be false", false);  
          }  
      
          @Test  
          public void testAssertNotNull() {  
            org.junit.Assert.assertNotNull("should not be null", new Object());  
          }  
      
          @Test  
          public void testAssertNotSame() {  
            org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());  
          }  
      
          @Test  
          public void testAssertNull() {  
            org.junit.Assert.assertNull("should be null", null);  
          }  
      
          @Test  
          public void testAssertSame() {  
            Integer aNumber = Integer.valueOf(768);  
            org.junit.Assert.assertSame("should be same", aNumber, aNumber);  
          }  
      
          // JUnit Matchers assertThat  
          @Test  
          public void testAssertThatBothContainsString() {  
            org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));  
          }  
      
          @Test  
          public void testAssertThathasItemsContainsString() {  
            org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));  
          }  
      
          @Test  
          public void testAssertThatEveryItemContainsString() {  
            org.junit.Assert.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())));  
          }  
    }

    3.assertThat

    assertThat( [value], [matcher statement] );

      value 是接下来想要测试的变量值;

            matcher statement 是使用Hamcrest 匹配符来表达的对前面变量所期望的值的声明,如果 value 值与 matcher statement 所表达的期望值相符,则测试成功,否则测试失败。

    //一般匹配符  
      
    // allOf匹配符表明如果接下来的所有条件必须都成立测试才通过,相当于“与”(&&)  
    assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) );  
    // anyOf匹配符表明如果接下来的所有条件只要有一个成立则测试通过,相当于“或”(||)  
    assertThat( testedNumber, anyOf( greaterThan(16), lessThan(8) ) );  
    // anything匹配符表明无论什么条件,永远为true  
    assertThat( testedNumber, anything() );  
    // is匹配符表明如果前面待测的object等于后面给出的object,则测试通过  
    assertThat( testedString, is( "developerWorks" ) );  
    // not匹配符和is匹配符正好相反,表明如果前面待测的object不等于后面给出的object,则测试通过  
    assertThat( testedString, not( "developerWorks" ) );  
      
    //字符串相关匹配符  
      
    // containsString匹配符表明如果测试的字符串testedString包含子字符串"developerWorks"则测试通过  
    assertThat( testedString, containsString( "developerWorks" ) );  
    // endsWith匹配符表明如果测试的字符串testedString以子字符串"developerWorks"结尾则测试通过  
    assertThat( testedString, endsWith( "developerWorks" ) );   
    // startsWith匹配符表明如果测试的字符串testedString以子字符串"developerWorks"开始则测试通过  
    assertThat( testedString, startsWith( "developerWorks" ) );   
    // equalTo匹配符表明如果测试的testedValue等于expectedValue则测试通过,equalTo可以测试数值之间,字  
    //符串之间和对象之间是否相等,相当于Object的equals方法  
    assertThat( testedValue, equalTo( expectedValue ) );   
    // equalToIgnoringCase匹配符表明如果测试的字符串testedString在忽略大小写的情况下等于  
    //"developerWorks"则测试通过  
    assertThat( testedString, equalToIgnoringCase( "developerWorks" ) );   
    // equalToIgnoringWhiteSpace匹配符表明如果测试的字符串testedString在忽略头尾的任意个空格的情况下等  
    //于"developerWorks"则测试通过,注意:字符串中的空格不能被忽略  
    assertThat( testedString, equalToIgnoringWhiteSpace( "developerWorks" ) );  
      
    //数值相关匹配符  
      
    // closeTo匹配符表明如果所测试的浮点型数testedDouble在20.0±0.5范围之内则测试通过  
    assertThat( testedDouble, closeTo( 20.0, 0.5 ) );  
    // greaterThan匹配符表明如果所测试的数值testedNumber大于16.0则测试通过  
    assertThat( testedNumber, greaterThan(16.0) );  
    // lessThan匹配符表明如果所测试的数值testedNumber小于16.0则测试通过  
    assertThat( testedNumber, lessThan (16.0) );  
    // greaterThanOrEqualTo匹配符表明如果所测试的数值testedNumber大于等于16.0则测试通过  
    assertThat( testedNumber, greaterThanOrEqualTo (16.0) );  
    // lessThanOrEqualTo匹配符表明如果所测试的数值testedNumber小于等于16.0则测试通过  
    assertThat( testedNumber, lessThanOrEqualTo (16.0) );  
      
    //collection相关匹配符  
      
    // hasEntry匹配符表明如果测试的Map对象mapObject含有一个键值为"key"对应元素值为"value"的Entry项则  
    //测试通过  
    assertThat( mapObject, hasEntry( "key", "value" ) );  
    // hasItem匹配符表明如果测试的迭代对象iterableObject含有元素“element”项则测试通过  
    assertThat( iterableObject, hasItem ( "element" ) );  
    // hasKey匹配符表明如果测试的Map对象mapObject含有键值“key”则测试通过  
    assertThat( mapObject, hasKey("key"));  
    // hasValue匹配符表明如果测试的Map对象mapObject含有元素值“value”则测试通过  
    assertThat( mapObject, hasValue("key"));
  • 相关阅读:
    VC++中使用ADO方式操作ACCESS数据库
    运维工程师必会的109个Linux命令
    linux上安装配置samba服务器
    ubuntu如何实现访问实际网络中windows共享文件夹
    R语言 入门知识--常用操作和例子
    坚持你选择的路
    scala eclipse plugin 插件安装
    Linux安装卸载Mysql数据库
    Hadoop HA高可用性架构和演进分析(转)
    Spring 系列: Spring 框架简介 -7个部分
  • 原文地址:https://www.cnblogs.com/Actexpler-S/p/8099192.html
Copyright © 2020-2023  润新知