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()等方法的区别,需要的朋友可以参考下 本 ...
随机推荐
- LintCode: Cosine Similarity
C++ class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @retu ...
- 使用instantclient_11_2 和PL/SQL Developer工具包连接oracle 11g远程数据库(转)
1,先到Oracle网站下载Instant Client : http://www.oracle.com/technology/global/cn/software/tech/oci/instantc ...
- Linq中的连接(join)
http://www.cnblogs.com/scottckt/archive/2010/08/11/1797716.html Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下 ...
- 〖Android〗利用droidsshd在Android手机中开启 sshd,sftp,..
源码下载地址: src: git clone https://code.google.com/p/droidsshd/ apk: http://droidsshd.googlecode.com/fil ...
- win10更新后无法远程,报 credssp加密oracle修正
答案都在图里,看不清就浏览器放大观看 打开开始菜单,搜索“编辑组策略” 进入
- spark的外排:AppendOnlyMap与ExternalAppendOnlyMap
相信很多人和我一样, 在控制台中总是可以看到会打印出如下的语句: INFO ExternalAppendOnlyMap: Thread 94 spilling in-memory map of 63. ...
- Linux下Shell元字符的释义
Linux下shell的巧妙应用,对系统的运维很有四两拨千斤的功效! Shell元字符 注意: () 在当前shell的子shell进程运行命令 {}在当前shell进程运行命令
- 自定义nsoperation的用法
#import <Foundation/Foundation.h> typedef void (^DownloadCompletionBlock)(UIImage *image); @in ...
- MySQL-join的实现原理、优化及NLJ算法
案例分析: select c.* from hotel_info_original c left join hotel_info_collection h on c.hotel_type=h.hote ...
- java struts2入门学习实例--用户注册
一.用户注册示例 register.jsp <%@ page language="java" contentType="text/html; charset=UT ...