1. 程式人生 > >Junit4.x高階用法詳解(一)

Junit4.x高階用法詳解(一)

最近整理程式碼的時候,總習慣把一些常用的工具類和方法等都寫在junit中,這樣可以方便於在想用的時候直接copy,在用junit的時候學到了一些比較有用的東西,記錄如下:

1.使用junit進行超時測試


    @Test(timeout=2000)
    public void testTimeout() throws InterruptedException {
        Thread.sleep(2000);
    }

    @Test(timeout=2000)
    public void testTimeout() throws InterruptedException {
        Thread.sleep(2001);
    }


2.使用junit進行異常測試

@Test(expected=IOException.class)
    public void testExceptions() throws InterruptedException {

        throw new RuntimeException();
    }
    
    
    @Test(expected=RuntimeException.class)
    public void testExceptions2() throws InterruptedException {
        
        throw new RuntimeException();
    }

3.使用junit進行引數測試

private SimpleDateFormat sdf;
    private String date;
    private String dateformat;
    private String expectedDate;
    
    
    
    public TestJunitParameter(String date, String dateformat,
            String expectedDate) {
        this.date = date;
        this.dateformat = dateformat;
        this.expectedDate = expectedDate;
    }
    
    @Parameters
    public static Collection getParamters() {

        String[][] object = {
                { "2011-07-01 00:20:20", "yyyyMMdd", "20110701" },
                { "2011-07-01 00:20:20", "yyyy年MM月dd日", "2011年07月01日" },
                { "2011-07-01 00:20:20", "HH時mm分ss秒", "00時20分20秒" } };
        List<String[]> list = Arrays.asList(object);
        return  list;
    }

    @Test
    public void testJunitParameter() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = df.parse(this.date);
        sdf = new SimpleDateFormat(this.dateformat);
        String result = sdf.format(d);
        Assert.assertEquals(this.expectedDate, result);

    }



4.使用junit進行Suite測試,不僅僅TestNg可以有suite哦~~~

@RunWith(Suite.class)
@SuiteClasses({TestDateFormat.class,TestIORead.class})
public class TestSuite {
    
    
}

5.使用junit進行mock測試

mock測試其實採用的Mockito進行,所以這裡不記錄了,將會有一個頁單獨的介紹Mockito.

6.使用@Category進行分類測試

public interface FastTests { /* category marker */ }
public interface SlowTests { /* category marker */ }

public class A {
  @Test
  public void a() {
    fail();
  }

  @Category(SlowTests.class)
  @Test
  public void b() {
  }
}

@Category({SlowTests.class, FastTests.class})
public class B {
  @Test
  public void c() {

  }
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
 // Will run A.b and B.c, but not A.a
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@ExcludeCategory(FastTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
  // Will run A.b, but not A.a or B.c
}

注:雖然Class B 也包含了SlowTests.class,但是其同時也包含了FastTests.class,因為我們在測試類的註解上加了@ExcludeCategory(FastTests.class),所以Class B的c方法是不會執行的


成就與否,15%在於個人的才幹和技能,而85%在於做人的技術和技巧。和大眾融洽地相處,以和諧取悅於人,留意尊重別人的立場,讓每個人都覺得自己是重要的,也就得到了討人喜歡的祕決了。