近期整理代码的时候,总习惯把一些经常使用的工具类和方法等都写在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进行分类測试

  1. public interface FastTests { /* category marker */ }
  2. public interface SlowTests { /* category marker */ }
  3.  
  4. public class A {
  5. @Test
  6. public void a() {
  7. fail();
  8. }
  9.  
  10. @Category(SlowTests.class)
  11. @Test
  12. public void b() {
  13. }
  14. }
  15.  
  16. @Category({SlowTests.class, FastTests.class})
  17. public class B {
  18. @Test
  19. public void c() {
  20.  
  21. }
  22. }
  23.  
  24. @RunWith(Categories.class)
  25. @IncludeCategory(SlowTests.class)
  26. @SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
  27. public class SlowTestSuite {
  28. // Will run A.b and B.c, but not A.a
  29. }
  30.  
  31. @RunWith(Categories.class)
  32. @IncludeCategory(SlowTests.class)
  33. @ExcludeCategory(FastTests.class)
  34. @SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
  35. public class SlowTestSuite {
  36. // Will run A.b, but not A.a or B.c
  37. }
  1.  

注:尽管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. Canvas中的非零围绕规则原理

    非零围绕规则:对于路径中指定范围区域,从该区域内部画一条足够长的线段.使此线段的全然落在路径范围之外. 非零围绕规则计数器:然后,将计数器初始化为0,每当这个线段与路径上的直线或曲线相交时,就改变计数 ...

  2. Linux多线程实践(六)使用Posix条件变量解决生产者消费者问题

    前面的一片文章我们已经讲过使用信号量解决生产者消费者问题.那么什么情况下我们须要引入条件变量呢? 这里借用  http://www.cnblogs.com/ngnetboy/p/3521547.htm ...

  3. m_Orchestrate learning system---二十四、thinkphp里面的ajax如何使用

    m_Orchestrate learning system---二十四.thinkphp里面的ajax如何使用 一.总结 一句话总结:其实ajax非常简单:前台要做的事情就是发送ajax请求过来,后台 ...

  4. [JSOI2008] [BZOJ1567] Blue Mary的战役地图 解题报告 (hash)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1567 Description Blue Mary最近迷上了玩Starcraft(星际争霸 ...

  5. 用jquery给select加选中事件

    select在前端开发过程中很常用,现在我们要实现一个效果,那就是选中select中的某一项,执行事件,本来自己没怎么接触过这些,最后网上找了一些资料,自己研究了一下,把方法分享给大家,大家如果有需要 ...

  6. 6.boostTCP通信

    客户端 #include <boost/asio.hpp> #include <iostream> #include <stdlib.h> using namesp ...

  7. POJ 2528 线段树

    坑: 这道题的坐标轴跟普通的坐标轴是不一样的-- 此题的坐标轴 标号是在中间的-- 线段树建树的时候就不用[l,mid][mid,r]了(这样是错的) 直接[l,mid][mid+1,r]就OK了 D ...

  8. OpenGL编程逐步深入(十一)组合变换

    准备知识 在前面的几节教程中,我们已经提到过几种变换,为物体在3D世界中的移动提供的极大的灵活性.但是我们还有很多东西需要学习(如摄像机控制和透视投影),你可以已经猜到,我们需要將这些变换组合起来.在 ...

  9. PostgreSQL Replication之第一章 理解复制概念(3)

    1.3 使用分片和数据分配 本节您将了解基本可扩展性技术,例如数据库分片.分片被广泛应用于高端系统并提供一个简单而且可靠的扩展设置方式来向外扩展.近年来,分片已经成为一种扩大专业系统规模的标准方式. ...

  10. vue中Object.defineProperty用法

    function def (obj, key, val, enumerable) { Object.defineProperty(obj, key, { value: val, enumerable: ...