package com.zy.junit.test;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.zy.service.ServiceTest;

/**
 *
 * junit 3有一些比较霸道的地方,表现在: 1.单元测试类必须继承自TestCase。2.要测试的方法必须以test开头。
 * 推荐使用junit 4 
 * (1)被测试的类必须放在包里(2)返回值必须为void,而且不能有任何入参,否则运行时异常
 * @author Administrator
 *
 */
public class Junit4Test
{

// 每个测试方法执行之前都要执行一次
 @Before
 public void before()
 {
  System.out.println("before");
 }

// 每个测试方法执行之后要执行一次
 @After
 public void after()
 {
  System.out.println("after");
 }

// 只在测试类实例化执行@BeforeClass方法一次,必须声明static
 @BeforeClass
 public static void beforeClass()
 {
  System.out.println("beforeClass");
 }

// 当所有测试执行完毕之后,执行@AfterClass一次,必须声明static
 @AfterClass
 public static void afterClass()
 {
  System.out.println("afterClass");
 }

// @Test元数据中的expected属性。expected属性的值是一个异常的类型,相当于try catch,只是吃掉了此异常
 @Test(expected = ArithmeticException.class)
 public void testExpected()
 {
  int a = 10 / 0;
  System.out.println(a);
 }

// 传入了一个时间(毫秒)给测试方法,如果测试方法在制定的时间之内没有运行完,则测试也失败。
 @Test(timeout = 500)
 public void testTimeout()
 {
  try
  {
   Thread.sleep(480);
  } catch (InterruptedException e)
  {
   e.printStackTrace();
  }
  System.out.println("timeout test");
 }

// 测试方法在测试中会被忽略,用于:测试的方法还没有实现时,保证不阻碍其他方法的测试
 @Ignore
 public void testIgnore()
 {
  System.out.println(new ServiceTest().getString("hello"));
 }

// 使用@Test标注,表明这是一个测试方法
 @Test
 public void testGetString()
 {
  System.out.println(new ServiceTest().getString("hello"));
 }

@Test
 public void testGetString2()
 {
  System.out.println(new ServiceTest().getString("hello2"));
 }

}

junit 用法实例的更多相关文章

  1. php中的curl使用入门教程和常见用法实例

    摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...

  2. 上传文件及$_FILES的用法实例

    Session变量($_SESSION):�php的SESSION函数产生的数据,都以超全局变量的方式,存放在$_SESSION变量中.1.Session简介SESSION也称为会话期,其是存储在服务 ...

  3. C++语言中cin cin.getline cin.get getline gets getchar 的用法实例

    #include <iostream> #include <string> using namespace std; //关于cin cin.getline cin.get g ...

  4. Union all的用法实例sql

    ---Union all的用法实例sqlSELECT TOP (100) PERCENT ID, bid_user_id, UserName, amount, createtime, borrowTy ...

  5. 【转】javascript入门系列演示·三种弹出对话框的用法实例

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  6. php strpos 用法实例教程

    定义和用法该strpos ( )函数返回的立场,首次出现了一系列内部其他字串. 如果字符串是没有发现,此功能返回FALSE . 语法 strpos(string,find,start) Paramet ...

  7. 【JSP】三种弹出对话框的用法实例

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  8. python多线程threading.Lock锁用法实例

    本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...

  9. jQuery中on()方法用法实例详解

    这篇文章主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能及各种常见的使用技巧,并对比分析了与bind(),live(),delegate()等方法的区别,需要的朋友可以参考下 本 ...

随机推荐

  1. jQuery 超屏加载

    jQuery 超屏加载,当文档超出屏幕的高度时,加载最新下个列数据 $(window).scroll(function () { var height = $(document).height(); ...

  2. Android Studio 之 Launch AVD 时" Intel HAXM is required to run this AVD, VT-x is disabled in BIOS; "

    问题描述:Launch AVD 时弹窗信息" Intel HAXM is required to run this AVD, VT-x is disabled in BIOS; " ...

  3. word你必须懂的。

    1.所有文字都有默认样式 2.可以调整样式,选择字体,级别,段落,缩进等设置

  4. server.xml引入子文件配置(tomcat虚拟主机)[转]

    在配置tomcat虚拟主机时候,如何每一个虚拟主机写成单独文件,server.xml包含这些子文件? 如以下<OneinStack>中,添加JAVA环境虚拟主机后tomcat配置文件详情: ...

  5. Spring组件扫描<context:component-scan/>详解

    引言 最近使用Spring,发现有很多依赖注入的内容,特别是DAO,百思不得其解,后来才知道是Spring的依赖注入.Spring可以批量将一个目录下所有的植入@Repository 注解或者@Ser ...

  6. [转]基本Guava工具

    转自:http://www.cnblogs.com/renchunxiao/p/3661918.html?utm_source=tuicool 使用Joiner类 将任意字符串通过分隔符进行连接到一起 ...

  7. python 爬虫资料

    API Requests PyQuery http://www.tuicool.com/articles/UZrmUb2 http://blog.csdn.net/cnmilan/article/de ...

  8. Nginx+Tomcat+Memcached 实现集群部署时Session共享

    Nginx+Tomcat+Memcached 实现集群部署时Session共享 一.简介 我们系统经常要保存用户登录信息,有Cookie和Session机制,Cookie客户端保存用户信息,Sessi ...

  9. GitHub如何下载clone指定的tag

    如上图,我想下载Tags标签为solution-4 的代码,如何处理呢? 命令如下: git clone --branch solution-4 git@github.com:zspo/learngi ...

  10. npm 主要命令

    本文主要参考自:http://www.runoob.com/nodejs/nodejs-npm.html 1.使用 npm 命令安装模块 $ npm install express var expre ...