一,背景知识:

由前面的知识可以知道:

/*
     * @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常用注解的更多相关文章

  1. Spring Boot学习(四)常用注解

    一.注解对照表 注解 使用位置 作用  @Controller  类名上方  声明此类是一个SpringMVC Controller 对象,处理http请求  @RequestMapping  类或方 ...

  2. spring学习十九 常用注解

    1. @Component 创建类对象,相当于配置<bean/>2. @Service 与@Component 功能相同. 2.1 写在 ServiceImpl 类上.3. @Reposi ...

  3. Linux学习(四)-Linux常用命令

    1.运行级别类 1.1运行级别说明: 0:关机 1:单用户[可用于找回丢失密码] 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重启 常用运行级 ...

  4. linux学习(四)-----linux常用指令

    touch 指令 touch 指令创建空文件 基本语法 touch 文件名称 应用实例 案例 1: 创建一个空文件 hello.txt cp 指令 cp 指令拷贝文件到指定目录 基本语法 cp [选项 ...

  5. Swagger2常用注解和使用方法

    一   引入maven依赖 <!--整合Swagger2--> <dependency> <groupId>com.spring4all</groupId&g ...

  6. Junit4学习使用和总结

    Junit4学习使用和总结 部分资料来源于网络 编辑于:20190710 一.Junit注解理解 1.@RunWith 首先要分清几个概念:测试方法.测试类.测试集.测试运行器.其中测试方法就是用@T ...

  7. Spring Boot入门(四):开发Web Api接口常用注解总结

    本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...

  8. Java 学习笔记 Junit4单元测试使用

    Junit使用 1.导入Junit包 到官网下载个Junit4.12.jar文件,放在lib目录 或者在类的空白处打@Test,之后按下alt+enter,选择添加Junit4依赖 之后就会弹出一个窗 ...

  9. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

  10. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

随机推荐

  1. 优先级队列Priority_queue

    定义 拥有权值观点的queue,,一个是返回最高优先级对象,一个是在底端添加新的对象.这种数据结构就是优先级队列(Priority Queue) . 实现 利用max_heap完成,以vector表现 ...

  2. WebService/WCF/WebAPI区别

    详细描述它们之间的区别,为什么这么选择,尤其是WCF vs WebAPI

  3. spring(四)之基于注解(Annotation-based)的配置.md

    注解 这里讲的注解有下面几个 @Autowired @Qualifier(" ") @Genre(" ") @Offline @Resource(name=&q ...

  4. Java获取精确到秒的时间戳(转)

    1.时间戳简介: 时间戳的定义:通常是一个字符序列,唯一地标识某一刻的时间.数字时间戳技术是数字签名技术一种变种的应用.是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01 ...

  5. wmic 获得系统硬件信息

    wmic扩展了wmi系统管理指令,提供了命令行接口和批处理执行系统管理的工具.通过别名机制将命令转为对wmi命名空间的操作 1.获得cpu信息 2.获得cpu 核数 3.获得内存条信息

  6. go web 第一天 学习笔记

    package main import ( "fmt" "log" "net/http" "strings" ) fun ...

  7. Ajax新玩法fetch API

    目前 Web 异步应用都是基于 XMLHttpRequest/ActiveXObject (IE)实现的, 这些对象不是专门为资源获取而设计的,因而它们的 API 非常复杂,同时还需要开发者处理兼容性 ...

  8. linux系统安装配置exim4(源码安装)

    一.Exim4概述 Exim是一个MTA(Mail Transfer Agent,邮件传输代理)服务器软件,该软件基于GPL协议开发,是一款开源软件.该软件主要运行于类UNIX系统.通常该软件会与Do ...

  9. 海外 App 的推送服务,试试 FCM 吧!!!

    > **版权声明:** > > **本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有.** > > **每周会统一更新到这里,如果喜欢,可关注公 ...

  10. RGB转MIPI CSI芯片方案TC358746XBG

    型号:TC358746XBG功能:RGB888/666/565与MIPI CSI 互转通信方式:IIC/SPI分辨率:720p电源:3.3/1.2V封装形式:BGA72深圳有现货库存,价格有优势,样片 ...