您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > junit
使用JUnit進(jìn)行單元測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2017/3/14 16:24:04 ] 推薦標(biāo)簽:Junit 單元測(cè)試

  異常測(cè)試
  你可以測(cè)試代碼是否它拋出了想要得到的異常。expected 參數(shù)和 @Test 注釋一起使用,F(xiàn)在讓我們看看活動(dòng)中的 @Test(expected)。
  @Test(expected = NullPointerException.class)
  public void testException() {
  throw new NullPointerException();
  }
  所有測(cè)試代碼
  代碼地址
package com.hollischuang.effective.unitest.service;
import org.junit.*;
/**
* @author Hollis 17/1/7.
*/
public class JUnitTest {
/**
* 只執(zhí)行一次,在整個(gè)類執(zhí)行之前執(zhí)行
*/
@BeforeClass
public static void beforeClass() {
System.out.println("in before class");
}
/**
* 只執(zhí)行一次,在整個(gè)類執(zhí)行之后執(zhí)行
*/
@AfterClass
public static void afterClass() {
System.out.println("in after class");
}
/**
* 每個(gè)測(cè)試方法被執(zhí)行前都被執(zhí)行一次
*/
@Before
public void before() {
System.out.println("in before");
}
/**
* 每個(gè)測(cè)試方法被執(zhí)行后都被執(zhí)行一次
*/
@After
public void after() {
System.out.println("in after");
}
// test case 1
@Test
public void testCase1() {
System.out.println("in test case 1");
}
// test case 2
@Test
public void testCase2() {
System.out.println("in test case 2");
}
/**
* 測(cè)試assertEquals
*/
@Test
public void testEquals() {
Assert.assertEquals(1 + 2, 3);
}
/**
* 測(cè)試assertTrue
*/
@Test
public void testTrue() {
Assert.assertTrue(1 + 2 == 3);
}
/**
* 測(cè)試assertFalse
*/
@Test
public void testFals() {
Assert.assertFalse(1 + 2 == 4);
}
/**
* 測(cè)試assertNotNull
*/
@Test
public void assertNotNull() {
Assert.assertNotNull("not null");
}
/**
* 測(cè)試assertNull
*/
@Test
public void assertNull() {
Assert.assertNull(null);
}
/**
* 測(cè)試fail和Ignore
*/
@Test
@Ignore
public void assertFail() {
Assert.fail();
}
/**
* 測(cè)試異常
*/
@Test(expected = NullPointerException.class)
public void testException() {
throw new NullPointerException();
}
/**
* 測(cè)試時(shí)間
*/
@Test(timeout = 1000)
public void testTimeoutSuccess() {
// do nothing
}
/**
* 測(cè)試時(shí)間
*/
@Test(timeout = 1000)
public void testTimeoutFailed() {
while (true) {
}
}
}
  總結(jié)
  本文主要介紹了JUnit的常見用法,后面會(huì)專門寫一篇文章介紹如何將JUnit和Spring集合到一起。

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd