近期整理代码的时候,总习惯把一些经常使用的工具类和方法等都写在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. UE4 中的人工智能解析—ShooterGame为例

    在UE4编辑器中,打开内容浏览器,右击鼠标,创建传说中的行为树: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQTM2MzA2MjM=/font/5a6L ...

  2. vijos - P1543极值问题(斐波那契数列 + 公式推导 + python)

    P1543极值问题 Accepted 标签:[显示标签] 背景 小铭的数学之旅2. 描写叙述 已知m.n为整数,且满足下列两个条件: ① m.n∈1,2.-,K ② (n^ 2-mn-m^2)^2=1 ...

  3. bzoj1193: [HNOI2006]马步距离(贪心+bfs)

    1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...

  4. struts2连接mysql多表查询

    下载地址:http://download.csdn.net/detail/qq_33599520/9786567 项目结构: 代码: package com.mstf.action; import j ...

  5. WebSocket handshake: Unexpected response code: 404

    在执行    http://www.cnblogs.com/best/p/5695570.html  提供的 websocket时候, 报错了 “WebSocket handshake: Unexpe ...

  6. JavaScript获取本机IP地址

    <script type="text/javascript"> /** * Get the user IP throught the webkitRTCPeerConn ...

  7. tensorflow 问题库

    1.module 'tensorflow.python.ops.nn' has no attribute 'rnn_cell' 将tf.nn.rnn_cell ->tf.contrib.rnn

  8. 解决mongodb TypeError: Cannot read property 'XXX' of null 问题

    有时候我们的字段里的数据为空而去查询就会报错. 比如以下形式: “cartList”:[] “cartList”:[{}] “cartList”:{} “cartList”:null 在查询的时候加上 ...

  9. 用xstart远程连接linux图形用户界面时发生已经在使用的情况

    1.举例打开pycharm 2.此时要输入ps aux|grep pycharm,出现下面的情况 3.然后找到矩形圈住的内容,然后输入 这样就杀掉了远程机子上pycharm,接着继续输入pycharm ...

  10. win2003系统同步Linux ntp server批处理

    最后更新时间: 2018/12/15 一般windows配置时间服务器,只需要在windows系统右下角,点时间,里面配置好对应NTP服务器地址就行, 至多再修改一下注册表 HKEY_LOCAL_MA ...