junit 用法实例
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 用法实例的更多相关文章
- php中的curl使用入门教程和常见用法实例
摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...
- 上传文件及$_FILES的用法实例
Session变量($_SESSION):�php的SESSION函数产生的数据,都以超全局变量的方式,存放在$_SESSION变量中.1.Session简介SESSION也称为会话期,其是存储在服务 ...
- C++语言中cin cin.getline cin.get getline gets getchar 的用法实例
#include <iostream> #include <string> using namespace std; //关于cin cin.getline cin.get g ...
- Union all的用法实例sql
---Union all的用法实例sqlSELECT TOP (100) PERCENT ID, bid_user_id, UserName, amount, createtime, borrowTy ...
- 【转】javascript入门系列演示·三种弹出对话框的用法实例
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- php strpos 用法实例教程
定义和用法该strpos ( )函数返回的立场,首次出现了一系列内部其他字串. 如果字符串是没有发现,此功能返回FALSE . 语法 strpos(string,find,start) Paramet ...
- 【JSP】三种弹出对话框的用法实例
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- python多线程threading.Lock锁用法实例
本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...
- jQuery中on()方法用法实例详解
这篇文章主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能及各种常见的使用技巧,并对比分析了与bind(),live(),delegate()等方法的区别,需要的朋友可以参考下 本 ...
随机推荐
- Read from socket failed: Connection reset by peer.
复制密钥另一台主机时,出现了错误: Read from socket failed: Connection reset by peer. 到被登录主机的/var/log/auth.log查看日志: M ...
- SqlDataAdapter概述
SqlDataAdapter是 DataSet和 SQL Server之间的桥接器,用于检索和保存数据.SqlDataAdapter通过对数据源使用适当的Transact-SQL语句映射 Fill(它 ...
- climbing-stairs-动态规划,爬楼梯的路径数
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Android中怎样做到自己定义的广播仅仅能有指定的app接收
今天没吊事.又去面试了,详细哪家公司就不说了,由于我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目.好吧,我错了... 事实上当我们已经在工作的时候.我们能够在空暇的时间 ...
- mysql从时间字符串中取出日期数据
addtime='2016-09-03 18:12:44' substr(addtime,1,10) as 创建日期 SUBSTR(string, string charcter, number o ...
- excel如何快速实现数据区域的框选
这里会存在2个情况,一个是快速的选择一行或者一列的数据,另外一个是快速的选择一块的数据(数据区域) 1.当有上万条数据时,怎么快速的选择一行或一列的数据? 方法:将鼠标放在需要选择的数据区域的开头位置 ...
- linux下kerberos教程
一.kerberos介绍 Kerberos这一名词来源于希腊神话“三个头的狗——地狱之门守护者”系统设计上采用客户端/服务器结构与DES加密技术,并且能够进行相互认证,即客户端和服务器端均可对对方进行 ...
- Nginx 日志改成 JSON 格式
Nginx 日志默认为普通文本的格式,例如,下面是 Nginx 的一行访问日志: 10.88.122.105 - - [02/Dec/2017:09:15:04 +0800] "GET /j ...
- ios中项目
上一个版本 http://pan.baidu.com/share/link?shareid=2183244558&uk=923776187 下载地址 http://pan.baidu.com ...
- MAVEN 搭建APPFUSE
2010-05-21 利用Maven构建appfuse. 步骤如下: 1)Maven下载,下载apache-maven-2.2.1-bin.zip 下载地址:http://apache.freela ...