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 ...
随机推荐
- C++算法接口使用参考
C++算法接口参考 算法参考:[algorithm] 编译:g++ -std=c++11 xxx_algorithm.cpp 运行:./a.out 1.保持原序列运算 all_of template ...
- Android 5.1.1在外置SD卡中创建文件夹
Android 4.4之后WRITE_MEDIA_STORAGE 权限仅提供给系统应用,不再授予第三方App,WRITE_EXTERNAL_STORAGE 权限,仅仅用于授权用户写 primary e ...
- css3多列及瀑布流效果
css3内容分块,多列效果(类似报纸版块排版): .div02{ /*count:内容分为几列,gap:内容与分割线的距离,rule:分割线样式*/ column-count:; column-gap ...
- 打造基于Clang LibTooling的iOS自动打点系统CLAS(一)
1. 手动打点的弊端 在很多ios工程师的日常工作中,不但要对接产品提出的功能性需求,还会收到产品出于数据统计分析需求目的而提出的附带的隐形需求:统计打点.大多数公司的基础框架层都会对统计打点功能做高 ...
- Bash中的测试——test, [], [[]], (())
转自: http://blog.csdn.net/u013961718 https://www.shiyanlou.com/courses/running test 和 [ ] test是一条内置命令 ...
- SVG交互动画制作
前面我们已经说过了要怎样制作CSS3动画,但是SVG动画一直都没有时间研究过,正好趁现在有一点时间静下心来研究一下. 一般来说,常见前端动画实现方案分为三种,CSS3动画,HTML动画(SVG动画), ...
- Apache的配置文件http.conf参数含义详解
Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) (1) 基本配置: ServerRoot "/mnt/s ...
- 【Eclipse】更改部署位置
在使用eclipse启动tomcat时,偶尔会遇到应用没被部署的现象,导致访问时出现404 问题原因:应用部署在了eclipse自带的webapps中. 我们通常不喜欢eclipse自带的tomcat ...
- Html中<Hr>标签、样式的使用和自定义设置
<Hr>标签中样式的使用和自定义设置... -------------------- ====================== 已经测试过了可以用的Hr样式: <!--这是一部分 ...
- Ta-lib函数功能列表
import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np impo ...