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. 微信小程序 - 滚动公告组件

    支持横轴.纵轴滚动. 点击下载:speaker

  2. 微信小程序 - this.triggerEvent()

    组件之间数据通信 调用组件wxml bind+组件内的方法名 <dialog bindclose="handleClose" bindopen="handleOpe ...

  3. Netcore使用MailKit进行邮件发送

    public void TestSendMailDemo() { var message = new MimeKit.MimeMessage(); message.From.Add(new MimeK ...

  4. Ubuntu安装rabbitMq

    笔者ubuntu版本为Ubuntu 15.10,查看ubuntu当前版本命令:cat /etc/issue. 由于rabbitMq需要erlang语言的支持,在安装rabbitMq之前需要安装erla ...

  5. JavaScript公共函数

    [在此处输入文章标题] // JScript 文件 /* ================================================================== JS 公 ...

  6. http 请求报文

    1.报文 2.http请求方法 restful接口 post:创建 put:更新

  7. Spring Remoting: Hessian

  8. Java之创建对象>7.Avoid finalizers

    1.Finalizers are unpredictable, often dangerous, and generally unnecessary. 2.never do anything time ...

  9. V-rep学习笔记:机器人模型创建3—搭建动力学模型

    接着之前写的V-rep学习笔记:机器人模型创建2—添加关节继续机器人创建流程.如果已经添加好关节,那么就可以进入流程的最后一步:搭建层次结构模型和模型定义(build the model hierar ...

  10. 917:Knight Moves

    题目链接:http://noi.openjudge.cn/ch0205/917/ 原题应该是hdu 1372 总时间限制: 1000ms  内存限制: 65536kB 描述 BackgroundMr ...