博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
junit 用法实例
阅读量:6975 次
发布时间:2019-06-27

本文共 1575 字,大约阅读时间需要 5 分钟。

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"));
 }

}

转载地址:http://xrkpl.baihongyu.com/

你可能感兴趣的文章
namenode如何存储复本?
查看>>
apache ab压力测试
查看>>
微信扫描二维码登入实现,网页端
查看>>
Python中is同一性运算符和==相等运算符区别
查看>>
ios项目文件结构 目录的整理
查看>>
javassist学习笔记
查看>>
JAVA中的并发工具 -- CountDownLatch、CyclicBarrier、Semaphore
查看>>
Dubbo原理何源码解析之服务暴露
查看>>
牛人学习记录
查看>>
python-range用法
查看>>
常用的正则表达式
查看>>
服务器的定义
查看>>
solrj操作单机solr
查看>>
Java架构演进之路
查看>>
chsop 兼容jquery(解决与transport.js冲突)
查看>>
tar常见文件解压法
查看>>
Oracle 表空间扩容
查看>>
python 查询 elasticsearch 常用方法(Query DSL)
查看>>
wordpress jquery加载如何实现?
查看>>
通过cat /proc/cpuinfo看处理器特点
查看>>