近期整理代码的时候,总习惯把一些经常使用的工具类和方法等都写在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. Codeforces 559B Equivalent Strings 等价串

    题意:给定两个等长串a,b.推断是否等价.等价的含义为:若长度为奇数,则必须是同样串.若长度是偶数,则将两串都均分成长度为原串一半的两个子串al,ar和bl,br,当中al和bl等价且ar和br等价, ...

  2. SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串

    [传送门:SPOJ1811&BZOJ2946] 简要题意: 给出若干个字符串,求出这些字符串的最长公共子串 题解: 后缀自动机 这两道题的区别只是在于一道给出了字符串个数,一个没给,不过也差不 ...

  3. BZOJ 1379 模拟退火

    模拟退火的第一题~ //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> u ...

  4. Python 从入门到精通 全程最佳实现梳理

    零零星星的时间,持续完善中...... 1.一些基础的必要信息归纳 Python 官网 www.python.org 发明者 吉多·范罗苏姆 发行时间 1991年,​26年前 编程泛型 多泛型.面向对 ...

  5. Swift学习笔记(5):集合类型

    目录: 数组:Array 集合:Set 字典:Dictionary Swift提供Array(有序集合数据).Set(无序无重复集合)和Dictionary(无序键值对集合)三种基本集合类型来存储明确 ...

  6. vue1.0父子、兄弟间 通信案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. salt的grains

    grains作用: 1.匹配 minion 2.收集信息 (每次重启minion才会收集) grains 数据存储在minion端. salt '*' grains.ls salt '*' grain ...

  8. php--防止DDos攻击代码

    <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...

  9. JavaScript知识复习

    JavaScript 数据类型 原始数据类型: number string boolean null undefined 对象:object: Function Array Date... 共有六种数 ...

  10. jQuery实现tab标签切换效果

    技巧一.jQuery :eq() 选择器 定义和用法 :eq() 选择器选取带有指定 index 值的元素. index 值从 0 开始,所有第一个元素的 index 值是 0(不是 1). 经常与其 ...