近期整理代码的时候,总习惯把一些经常使用的工具类和方法等都写在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%在于做人的技术和技巧。和大众融洽地相处。以和谐取悦于人,留意尊重别人的立场,让每一个人都认为自己是重要的,也就得到了讨人喜欢的秘决了。

Junit4.x高级使用方法具体解释(一)的更多相关文章

  1. Js apply 方法 具体解释

    Js apply方法具体解释 我在一開始看到javascript的函数apply和call时,很的模糊,看也看不懂,近期在网上看到一些文章对apply方法和call的一些演示样例,总算是看的有点眉目了 ...

  2. Format类及其子类功能和使用方法具体解释

    Format类及其子类功能和使用方法具体解释 1.   Format类结构: ·        java.lang.Object ·        java.text.Format ·         ...

  3. hbase-0.94安装方法具体解释

    先决条件:     1)java环境,须要安装java1.6以上版本号     2)hadoop环境.因为HBase架构是基于其它文件存储系统的,因此在分布式模式下安装Hadoop是必须的,可是,假设 ...

  4. ZooKeeper安装方法具体解释

    ZooKeeper安装方式分为两种,一种为单机模式.一个为集群模式,集群模式须要事先正确配置hadoop集群,安装方法參考hadoop-1.2.1安装方法具体解释 单机模式安装: 1.上传并解压zoo ...

  5. 模式识别 - libsvm的函数调用方法 具体解释

    libsvm的函数调用方法 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26261173 须要载入(load)SVM的 ...

  6. const 使用方法具体解释

    const使用方法具体解释 面向对象是C++的重要特性.  可是c++在c的基础上新添加的几点优化也是非常耀眼的 就const直接能够代替c中的#define 下面几点非常重要,学不好后果也也非常严重 ...

  7. hive-0.11.0安装方法具体解释

    先决条件:     1)java环境,须要安装java1.6以上版本号     2)hadoop环境,Hadoop-1.2.1的安装方法參考hadoop-1.2.1安装方法具体解释 本文採用的hado ...

  8. Vue 事件的高级使用方法

    Vue 事件的高级使用方法 事件方法 在Vue中提供了4中事件监听方法,分别是: $on(event: string | Array, fn) $emit(event: string) $once(e ...

  9. 第44篇-为native方法设置解释执行入口

    对于Java中的native方法来说,实际上调用的是C/C++实现的本地函数,由于可能会在Java解释执行过程中调用native方法,或在本地函数的实现过程中调用Java方法,所以当两者相互调用时,必 ...

随机推荐

  1. HDU 5344(MZL&#39;s xor-(ai+aj)的异或和)

    MZL's xor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  2. LSTM 时间序列数据的异常检测

    见 http://www.infoq.com/cn/articles/deep-learning-time-series-anomaly-detection 但是不够详细

  3. hpuoj--校赛--2015年的第一场雪(暴力)

    问题 D: 感恩节KK专场--2015年的第一场雪 时间限制: 1 Sec  内存限制: 128 MB 提交: 865  解决: 76 [提交][状态][讨论版] 题目描述 下雪了,KK学长站在三教门 ...

  4. POJ 3661 DP

    题意: 思路: i表示到了i,j表示疲劳度为j f[i][j]表示能跑的最大距离 f[i][j]=f[i-1][j-1]+a[i] if(i-j>=0)f[i][0]=max(f[i][0],f ...

  5. SFTP的使用

    SFTP的使用: 1.项目中需要引入jar包,下载地址:https://sourceforge.net/projects/jsch/files/jsch.jar/ 2.需要下载SFTP服务器,下载地址 ...

  6. koda java

    https://kodejava.org/category/spring/spring-jdbc/

  7. vuejs scope

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. <Sicily>数字反转

    一.题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 二.输入 输入共 1 行, ...

  9. 三分钟学会用SpringMVC搭建最小系统(超详细)_转载

    前言 做 Java Web 开发的你,一定听说过SpringMVC的大名,作为现在运用最广泛的Java框架,它到目前为止依然保持着强大的活力和广泛的用户群. 本文介绍如何用eclipse一步一步搭建S ...

  10. UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...