Junit4学习(四)Junit4常用注解
一,背景知识:
由前面的知识可以知道:
/*
* @Test:将一个普通方法修饰为一个测试方法
* @Test(exception=XXX.class)
* @Test(time=毫秒)
* @BeforeClass:它会在所有的测试方法前被执行,static修饰
* @AfterClass:它会在所有的测试方法后被执行,static修饰
* @Before:它会在每一个测试方法前被执行一次
* @After:它会在每一个测试方法后被执行一次
* @Ignore:省略
* @RunWith:修改运行器org。junit。runner。Runner
*
* */
其实@Test不仅可以修饰一个普通方法为测试方法,还可以获取异常或者控制测试方法的执行时间
二,@Test的功能
A,获取异常
B,控制测试代码执行时间
A,获取异常代码展示
1,获取异常,对异常的捕获:@Test(expected=XXX.class)
package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test(expected=ArithmeticException.class)
public void testDivide(){
assertEquals(4, new Calculate().divide(12, 0));
} }
运行后结果:
2,没有通过@Test(expected=ArithmeticException.class)注解时代码以及结果:
package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test
public void testDivide(){
assertEquals(4, new Calculate().divide(12, 0));
} }
运行结果:
B,控制测试代码执行时间,代码展示
测试方法控制@Test(timeout=毫秒),主要是针对代码中有循环代码的测试控制或者超时运行不符合预期的判定
1,我们使用对一个死循环进行测试:
package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test(timeout=2000)
public void testWhile(){
while(true){
System.out.println("run forever...");
}
}
}
结果及时运行2秒后系统自动停止运行;
2,让当前线程运行2000毫秒,测试代码运行3000毫秒,符合预期结果
package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test(timeout=3000)
public void testReadFile(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果通过;
也可以通过调整测试时间比线程时间小,测试不符合预期的场景;
三,Ignore注解(该注解可以忽略当前的运行的方法,有时候改测试方法没有实现或者以后再实现)
package com.duo.util; import static org.junit.Assert.*; import org.junit.Ignore;
import org.junit.Test; public class Anotation { @Test(expected=ArithmeticException.class)
public void testDivide(){
assertEquals(4, new Calculate().divide(12, 0));
} @Ignore
@Test(timeout=2000)
public void testWhile(){
while(true){
System.out.println("run forever...");
}
} @Test(timeout=3000)
public void testReadFile(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
四,RunWith,可以修改测试运行器:org.junit.runner.Runner(后面使用到再解释)
五,断言:assert
断言assert的好多方法可以直接使用,主要是使用了静态导入:import static org.junit.Assert.*;
Junit4学习(四)Junit4常用注解的更多相关文章
- Spring Boot学习(四)常用注解
一.注解对照表 注解 使用位置 作用 @Controller 类名上方 声明此类是一个SpringMVC Controller 对象,处理http请求 @RequestMapping 类或方 ...
- spring学习十九 常用注解
1. @Component 创建类对象,相当于配置<bean/>2. @Service 与@Component 功能相同. 2.1 写在 ServiceImpl 类上.3. @Reposi ...
- Linux学习(四)-Linux常用命令
1.运行级别类 1.1运行级别说明: 0:关机 1:单用户[可用于找回丢失密码] 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重启 常用运行级 ...
- linux学习(四)-----linux常用指令
touch 指令 touch 指令创建空文件 基本语法 touch 文件名称 应用实例 案例 1: 创建一个空文件 hello.txt cp 指令 cp 指令拷贝文件到指定目录 基本语法 cp [选项 ...
- Swagger2常用注解和使用方法
一 引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...
- Junit4学习使用和总结
Junit4学习使用和总结 部分资料来源于网络 编辑于:20190710 一.Junit注解理解 1.@RunWith 首先要分清几个概念:测试方法.测试类.测试集.测试运行器.其中测试方法就是用@T ...
- Spring Boot入门(四):开发Web Api接口常用注解总结
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...
- Java 学习笔记 Junit4单元测试使用
Junit使用 1.导入Junit包 到官网下载个Junit4.12.jar文件,放在lib目录 或者在类的空白处打@Test,之后按下alt+enter,选择添加Junit4依赖 之后就会弹出一个窗 ...
- springmvc学习笔记(常用注解)
springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
随机推荐
- # webpack 3 & React 的简单配置 。
今天真是难过的一天
- python-ansible api2.0 多进程执行不同的playbook
自动化运维工具:ansible 多进程调用ansible api的应用场景: 应用系统检查 一个应用系统可能具有20—50台服务器的集群,初步的系统层面检查可以用一个统一的playbook来检查, ...
- 前后端分离(手)-- 使用mock.js(好样的)
## 前言: 本篇博文昨天七夕写的,一天下来被虐得体无完肤,苦逼的单身狗只能学习,对!我爱学习,关掉朋友圈,并写了一篇博文发泄发泄.这次写mock.js的使用,能使前后端分离,分离,分离,重要的是说三 ...
- Andrew Ng机器学习课程笔记--week11(图像识别&总结划重点)
一.内容概要 Photo OCR Problem Decription and pipeline(问题描述和流程图) Sliding Windows(滑动窗口) Getting Lots of Dat ...
- SpringMVC参数绑定(从请求中接受参数)
参数绑定(从请求中接收参数) 1)默认类型: 在controller方法中可以有也可以没有,看自己需求随意添加. httpservletRqeust,httpServletResponse,httpS ...
- [WPF]如何调试Data Binding
前言 在WPF开发中,将ViewModel中对象绑定到UI上时,会出现明明已经将数据对象Binding到UI,但是UI上就是不显示等等的问题.这篇博客将介绍WPF Data Binding相关的内容, ...
- jmeter性能测试 套路一
真的跑压力,都是master-slave的方式,部署在压力机上跑性能测试 本机一般都是调试.
- 前端开发【第一篇:HTML】
HTML初识 1.什么是HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都 ...
- c# typeof 与 GetType 作用与区别
官方解释: Used to obtain the "System.Type" object for a type. A 'typeof' expression takes the ...
- 【转义字符】HTML 字符实体< >: &
在开发中遇到javascript从后台获取的url 会被转义,如:http://localhost:8080/Home/Index?a=14&b=15&c=123,想把它转成http: ...