zoukankan      html  css  js  c++  java
  • 单元测试(二)

    前言:junit 介绍。转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9405519.html

    二、 java单测工具

    junit4介绍

    Java用的最多的单测框架,使用非常简单,主流IDE基本都集成了JUnit,具体用法看看官方文档http://junit.org/junit4/

    常用注解

      @Before:初始化方法,在任何一个测试方法执行之前,必须执行的代码。对比 JUnit 3 ,和 setUp()方法具有相同的功能。在该注解的方法中,可以进行一些准备工作,比如初始化对象,打开网络连接等。

      @After:释放资源,在任何一个测试方法执行之后,需要进行的收尾工作。对比 JUnit 3 ,和 tearDown()方法具有相同的功能。

      @Test:测试方法,表明这是一个测试方法。在 JUnit 中将会自动被执行。对与方法的声明也有如下要求:名字可以随便取,没有任何限制,但是返回值必须为 void ,而且不能有任何参数。如果违反这些规定,会在运行时抛出一个异常。不过,为了培养一个好的编程习惯,我们一般在测试的方法名上加 test ,比如:testAdd()。同时,该 Annotation(@Test) 还可以测试期望异常和超时时间,如 @Test(timeout=100),我们给测试函数设定一个执行时间,超过这个时间(100毫秒),他们就会被系统强行终止,并且系统还会向你汇报该函数结束的原因是因为超时,这样你就可以发现这些 bug 了。而且,它还可以测试期望的异常,例如,我们刚刚的那个空指针异常就可以这样:@Test(expected=NullPointerException.class)。

       @Ignore:忽略的测试方法,标注的含义就是“某些方法尚未完成,咱不参与此次测试”;这样的话测试结果就会提示你有几个测试被忽略,而不是失败。一旦你完成了相应的函数,只需要把 @Ignore 注解删除即可,就可以进行正常测试了。当然,这个 @Ignore 注解对于像我这样有“强迫症”的人还是大有意义的。每当看到红色条(测试失败)的时候就会全身不舒服,感觉无法忍受(除非要测试的目的就是让它失败)。当然,对代码也是一样,无法忍受那些杂乱不堪的代码。

      @BeforeClass:针对所有测试,也就是整个测试类中,在所有测试方法执行前,都会先执行由它注解的方法,而且只执行一次。当然,需要注意的是,修饰符必须是 public static void xxxx ;此 Annotation 是 JUnit 4 新增的功能。

      @AfterClass:针对所有测试,也就是整个测试类中,在所有测试方法都执行完之后,才会执行由它注解的方法,而且只执行一次。当然,需要注意的是,修饰符也必须是 public static void xxxx ;此 Annotation 也是 JUnit 4 新增的功能,与 @BeforeClass 是一对。

    执行顺序

       在 JUnit 4 中,单元测试用例的执行顺序为:

      @BeforeClass、@Before、@Test、@After、@AfterClass

    assertThat 是什么?

      建议直接看官方文档吧,https://github.com/junit-team/junit4/wiki/Matchers-and-assertthat

      在org.junit.Assert中除了常用的相等、布尔、非空等断言,还有一种assertThat,需要配合org.hamcrest.Matcher使用,这种断言的语法为:

      assertThat([reason, ]T actual, Matcher<? super T> matcher),其中,reason为断言失败时的输出信息,actual为断言的值或对象,matcher为断言的匹配器,里面的逻辑决定了给定的actual对象满不满足断言。

      在org.hamcrest.CoreMatchers类中组织了所有JUnit内置的Matcher,调用其任意一个方法都会创建一个与方法名字相关的Matcher。

      我认为assertThat+Matcher是为了代码的可读性更好,也支持用户自定义断言。

    org.hamcrest.Matcher接口的源码

    /** 
     * A matcher over acceptable values. 
     * A matcher is able to describe itself to give feedback when it fails. 
     * <p/> 
     * Matcher implementations should <b>NOT directly implement this interface</b>. 
     * Instead, <b>extend</b> the {@link BaseMatcher} abstract class, 
     * which will ensure that the Matcher API can grow to support 
     * new features and remain compatible with all Matcher implementations. 
     * <p/> 
     * For easy access to common Matcher implementations, use the static factory 
     * methods in {@link CoreMatchers}. 
     * <p/> 
     * N.B. Well designed matchers should be immutable. 
     *  
     * @see CoreMatchers 
     * @see BaseMatcher 
     */  
    public interface Matcher<T> extends SelfDescribing {  
      
        boolean matches(Object item);  
          
        void describeMismatch(Object item, Description mismatchDescription);  
      
        @Deprecated  
        void _dont_implement_Matcher___instead_extend_BaseMatcher_();  
    }

      注释中有提到自定义Matcher不应该直接实现这个接口,而应该继承org.hamcrest.BaseMatcher抽象类

     Runner

      JUnit的Runner是指继承了org.junit.runner.Runner的类,是用来真正执行单测方法的类,JUnit有提供默认的Runner,如org.junit.runners.JUnit4,也可以通过@RunWith注解来指定自定义的Runner,如@RunWith(SpringRunner.class)

      参考:https://blog.csdn.net/baiyun8789/article/details/72876597

      Runner的介绍还可以参考这篇文章: https://www.mscharhag.com/java/understanding-junits-runner-architecture

    Rule

      JUnit自带了一些方便使用的Rule实现,可以参考下面的文档 https://github.com/junit-team/junit4/wiki/Rules

    Examples  

    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("failure - strings are not equal", "text", "text");
      }
    
      @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() {
        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(768);
        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);
      }
    }

     

  • 相关阅读:
    icons、tabs、tabs2、toolbars例子详解
    list、listhorizontal、listsearch的理解
    forms、forms_toolbar例子理解
    Accelerometer
    audio,carousel的学习
    数组去空项,相同项
    require与include函数
    php下intval()和(int)转换使用与区别
    PHP需要拷到system32下的加载库及一些相关知识
    字符转义
  • 原文地址:https://www.cnblogs.com/yuxiaole/p/9405519.html
Copyright © 2011-2022 走看看