近期整理代码的时候,总习惯把一些经常使用的工具类和方法等都写在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. 迷宫求解_数据结构c语言版

    #include <iostream> #include <string> #include <cstdio> #include <cstdlib> # ...

  2. codeforces@281 B

    shui #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  3. poj--2083--Fractal(dfs)

    Fractal Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  4. python 字符串匹配问题

    想匹配html = <div class="back fl"><a href="javascript:void(0);" onclick=&q ...

  5. 《剑指offer》树的子结构

    一.题目描述 输入两颗二叉树A,B,判断B是不是A的子结构. 二.输入描述 输入两棵树A,B. 三.输出描述 判断B是不是A的子结构,是返回true,否则返回false. 四.牛客网提供的框架 /* ...

  6. docker操作大全

    docker 常用操作方法 查看docker版本docker version 搜索镜像docker serach 镜像名称 拉去镜像docker pull 镜像名称 查看本地镜像仓库信息docker ...

  7. caioj 1087 动态规划入门(非常规DP11:潜水员)(二维背包)

    这道题的难点在于价值可以多. 这道题我一开始用的是前面的状态推现在的状态 实现比较麻烦,因为价值可以多,所以就设最大价值 为题目给的最大价值乘以10 #include<cstdio> #i ...

  8. UVALive 2659 数独 DLX模板

    建图: 从1到16枚举所有的行.列上放的数. 代码: #include <iostream> #include <cstdio> #include <cstring> ...

  9. JavaScript 回车键绑定登录 事件 常用键位码(keyCode)

    1.回车键绑定登录事件 $(document).keydown(function (e) { if ((e.keyCode || e.which) == 13) { //document.queryS ...

  10. Atitit.软件开发的终于的设计&#160;dsl化,ast化(建立ast,&#160;解析运行ast)

    Atitit.软件开发的终于的设计 dsl化,ast化(建立ast, 解析运行ast) 1. 使用js,html 撰写dsl 1 1.1. 架构图 1 1.2. html 2 1.3. Js 2 1. ...