Junit使用教程

來源:互聯網
上載者:User

標籤:div   failure   nta   字串   ack   堆棧   down   mcr   art   

二、核心——斷言

斷言是編寫測試案例的核心實現方式,即期望值是多少,測試的結果是多少,以此來判斷測試是否通過。

1. 斷言核心方法

assertArrayEquals(expecteds, actuals) 查看兩個數組是否相等。
assertEquals(expected, actual) 查看兩個對象是否相等。類似於字串比較使用的equals()方法
assertNotEquals(first, second) 查看兩個對象是否不相等。
assertNull(object) 查看對象是否為空白。
assertNotNull(object) 查看對象是否不為空白。
assertSame(expected, actual) 查看兩個對象的引用是否相等。類似於使用“==”比較兩個對象
assertNotSame(unexpected, actual) 查看兩個對象的引用是否不相等。類似於使用“!=”比較兩個對象
assertTrue(condition) 查看運行結果是否為true。
assertFalse(condition) 查看運行結果是否為false。
assertThat(actual, matcher) 查看實際值是否滿足指定的條件
fail() 讓測試失敗

2. 樣本

  1.  package test;
  2.   
  3.  import static org.hamcrest.CoreMatchers.*;
  4.  import static org.junit.Assert.*;
  5.   
  6.  import java.util.Arrays;
  7.   
  8.  import org.hamcrest.core.CombinableMatcher;
  9.  import org.junit.Test;
  10.   
  11.  public class AssertTests {
  12.   
  13.  @Test
  14.  public void testAssertArrayEquals() {
  15.  byte[] expected = "trial".getBytes();
  16.  byte[] actual = "trial".getBytes();
  17.  org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);
  18.  }
  19.   
  20.  @Test
  21.  public void testAssertEquals() {
  22.  org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);
  23.  }
  24.   
  25.  @Test
  26.  public void testAssertFalse() {
  27.  org.junit.Assert.assertFalse("failure - should be false", false);
  28.  }
  29.   
  30.  @Test
  31.  public void testAssertNotNull() {
  32.  org.junit.Assert.assertNotNull("should not be null", new Object());
  33.  }
  34.   
  35.  @Test
  36.  public void testAssertNotSame() {
  37.  org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());
  38.  }
  39.   
  40.  @Test
  41.  public void testAssertNull() {
  42.  org.junit.Assert.assertNull("should be null", null);
  43.  }
  44.   
  45.  @Test
  46.  public void testAssertSame() {
  47.  Integer aNumber = Integer.valueOf(768);
  48.  org.junit.Assert.assertSame("should be same", aNumber, aNumber);
  49.  }
  50.   
  51.  // JUnit Matchers assertThat
  52.  @Test
  53.  public void testAssertThatBothContainsString() {
  54.  org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
  55.  }
  56.   
  57.  @Test
  58.  public void testAssertThathasItemsContainsString() {
  59.  org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
  60.  }
  61.   
  62.  @Test
  63.  public void testAssertThatEveryItemContainsString() {
  64.  org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
  65.  }
  66.   
  67.  // Core Hamcrest Matchers with assertThat
  68.  @Test
  69.  public void testAssertThatHamcrestCoreMatchers() {
  70.  assertThat("good", allOf(equalTo("good"), startsWith("good")));
  71.  assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
  72.  assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
  73.  assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
  74.  assertThat(new Object(), not(sameInstance(new Object())));
  75.  }
  76.  }

三、核心——註解

1. 說明

@Before 初始化方法
@After 釋放資源
@Test 測試方法,在這裡可以測試期望異常和逾時時間
@Ignore 忽略的測試方法
@BeforeClass 針對所有測試,只執行一次,且必須為static void
@AfterClass 針對所有測試,只執行一次,且必須為static void
@RunWith 指定測試類別使用某個運行器
@Parameters 指定測試類別的測試資料集合
@Rule 允許靈活添加或重新定義測試類別中的每個測試方法的行為
@FixMethodOrder 指定測試方法的執行順序

2. 執行順序

一個測試類別單元測試的執行順序為:

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

每一個測試方法的調用順序為:

@Before –> @Test –> @After

3. 樣本

  1.  package test;
  2.   
  3.  import static org.junit.Assert.*;
  4.   
  5.  import org.junit.*;
  6.   
  7.  public class JDemoTest {
  8.   
  9.  @BeforeClass
  10.  public static void setUpBeforeClass() throws Exception {
  11.  System.out.println("in BeforeClass================");
  12.  }
  13.   
  14.  @AfterClass
  15.  public static void tearDownAfterClass() throws Exception {
  16.  System.out.println("in AfterClass=================");
  17.  }
  18.   
  19.  @Before
  20.  public void before() {
  21.  System.out.println("in Before");
  22.  }
  23.   
  24.  @After
  25.  public void after() {
  26.  System.out.println("in After");
  27.  }
  28.   
  29.  @Test(timeout = 10000)
  30.  public void testadd() {
  31.  JDemo a = new JDemo();
  32.  assertEquals(6, a.add(3, 3));
  33.  System.out.println("in Test ----Add");
  34.  }
  35.   
  36.  @Test
  37.  public void testdivision() {
  38.  JDemo a = new JDemo();
  39.  assertEquals(3, a.division(6, 2));
  40.  System.out.println("in Test ----Division");
  41.  }
  42.   
  43.  @Ignore
  44.  @Test
  45.  public void test_ignore() {
  46.  JDemo a = new JDemo();
  47.  assertEquals(6, a.add(1, 5));
  48.  System.out.println("in test_ignore");
  49.  }
  50.   
  51.  @Test
  52.  public void teest_fail() {
  53.  fail();
  54.  }
  55.  }
  56.   
  57.  class JDemo extends Thread {
  58.   
  59.  int result;
  60.   
  61.  public int add(int a, int b) {
  62.  try {
  63.  sleep(1000);
  64.  result = a + b;
  65.  } catch (InterruptedException e) {
  66.  }
  67.  return result;
  68.  }
  69.   
  70.  public int division(int a, int b) {
  71.  return result = a / b;
  72.  }
  73.  }

執行結果:

  1.  in BeforeClass================
  2.  in Before
  3.  in Test ----Add
  4.  in After
  5.  in Before
  6.  in Test ----Division
  7.  in After
  8.  in AfterClass=================

 

圖中左上紅框中部分表示Junit運行結果,5個成功(1個忽略),1個錯誤,1個失敗。(注意錯誤和失敗不是一回事,錯誤說明代碼有錯誤,而失敗表示該測試方法測試失敗)

左下紅框中則表示出了各個測試方法的運行狀態,可以看到成功、錯誤、失敗、失敗各自的表徵圖是不一樣的,還可以看到已耗用時間。

右邊部分則是異常堆棧,可查看異常資訊。

 

原文:9628449

Junit使用教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.