org.junit.Test
@Target(value={METHOD})    - - - target注解:有效目标--只作用于方法
@Retention(value=RUNTIME)  - - - Retention注解:有效范围--运行时

The Test annotation tells JUnit that thepublic void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

(test注解告诉Junit为要做测试的public权限的方法创建一个测试实例。为了运行这个方法,Junit首先会创建一个该类实例,用于调用被@Test注解了的方法。遇到任何异常抛出时,Junit都会执行失败。如果没有异常抛出,Junit测试必然会成功)

A simple test looks like this:   (简单实例如下:)

 public class Example {@Testpublic void method() {org.junit.Assert.assertTrue( new ArrayList().isEmpty() );}}

The Test annotation supports two optional parameters. The first,expected, declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails.

(test注解可支持两个可选参数。第一个参数为:excepted----声明被测试方法应该抛出的异常。如果没有抛出异常或抛出的异常不是声明的异常,则会测试失败。)

For example, the following test succeeds:  (比如,下面的成功测试:)

    @Test(expected=IndexOutOfBoundsException.class) public void outOfBounds() {new ArrayList<Object>().get(1);}

If the exception's message or one of its properties should be verified, theExpectedException rule can be used. Further information about exception testing can be found at theJUnit Wiki.

(如果要验证异常信息或某个属性,可是使用ExpectedException的规则(即在这种情况下推荐使用Exception参数)。若要了解exception测试的详细信息可以去JUnit Wiki 中查看。)


The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).

(第二个可选参数:timeout--如果单元测试的实际超过了规定的时间,则会显示测试失败。单位:毫秒)

The following test fails:    (如下,测试失败情况:)

    @Test(timeout=100) public void infinity() {while(true);}


Warning: while timeout is useful to catch and terminate infinite loops, it shouldnot be considered deterministic. The following test may or may not fail depending on how the operating system schedules threads:

(警告:尽管timeout对于捕获和终止无限循环体非常有用,但应该考虑其不确定性。下面的测试可能会或可能不会失败,这个要根据运行系统的线程决定。)

    @Test(timeout=100) public void sleep100() {Thread.sleep(100);}


THREAD SAFETY WARNING: Test methods with a timeout parameter are run in a thread other than the thread which runs the fixture's @Before and @After methods. This may yield different behavior for code that is not thread safe when compared to the same test method without a timeout parameter.Consider using the org.junit.rules.Timeout rule instead, which ensures a test method is run on the same thread as the fixture's @Before and @After methods.

Since:
4.0

(线程安全性警告:@Test方法的参数timeout只作用于一个线程上,除了运行时固定会执行的@Before和@After 方法的线程。。。。。)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@Test注解:只能作用于public权限的方法

解读Junit的@Test注解,避免initializationerror相关推荐

  1. 自动化测试(二) 单元测试junit的Test注解突然不能使用原因以及解决方案

    自动化测试(二) 单元测试junit的Test注解突然不能使用原因以及解决方案 参考文章: (1)自动化测试(二) 单元测试junit的Test注解突然不能使用原因以及解决方案 (2)https:// ...

  2. JUnit 5 常用注解与方法

    JUnit 5 常用注解与方法 断言相等 断言不相等 断言数组相等 断言数组不相等 断言某值为真 断言某值为假 断言某值为 null 断言某值不为 null 断言会抛出异常 断言不会抛出异常 注解: ...

  3. Junit测试常用注解

    Junit常用注解 0x01 摘要 本文简要说下junit里面常用注解的含义和使用,还会总结一些常用的Assert判断语句. 0x02 常用Junit注解 2.1 @Test 用在方法上,定义该方法是 ...

  4. JUnit4 与 JUnit 5 常用注解对比

    JUnit4 与 JUnit 5 常用注解对比 JUnit4 JUnit5 说明 @Test @Test 表示该方法是一个测试方法.JUnit5与JUnit 4的@Test注解不同的是,它没有声明任何 ...

  5. java Junit 为什么@Test注解里的方法必须是public void修饰的

    通过查阅了Junit的源码,里面有段代码是这样的 1 protected void validateInstanceMethods(List < Throwable > errors) { ...

  6. junit 引入spring 注解管理_第05章 Spring 整合 Junit

    3.1 测试类中的问题和解决思路 3.1.1 问题 在测试类中,每个测试方法都有以下两行代码: ApplicationContext 这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常.所 ...

  7. 七、Web服务器——Junit单元测试 反射 注解学习笔记

    今日内容 1. Junit单元测试 2. 反射 3. 注解 Junit单元测试: * 测试分类:1. 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值.2. 白盒测试:需要写代码的.关注程 ...

  8. 【JUnit 报错】 method initializationerror not found:JUnit4单元测试报错问题

    今天是用JUnit测试一段代码,报错method initializationerror not found::出现如下问题: 双击这个就显示出现如下的错误: 查询网上,说是junit版本的问题: 那 ...

  9. Junit单元测试/反射/注解

    Junit单元测试: 测试分类: 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 白盒测试:需要写代码的.关注程序具体的执行流程. Junit使用:白盒测试 步骤: 定义一个测试类(测 ...

最新文章

  1. DeepMind 的2017:有 AlphaGo,更有社会责任
  2. Python2和python3字符编码的区别
  3. 文件与目录权限,文件查找
  4. Spring Cloud Feign 负载均衡
  5. 安装LR11,到执行产品配置时,停止不动了
  6. java基础——static 关键字小节
  7. 为何python不好找工作-Python培训班出来找工作容易吗?老男孩教育
  8. 2008.5调试安装hp dl385 两台hp dl585
  9. CodeRush For .Net 使用-------使用标签
  10. The nodejs-legacy package simply installs a symbolic link so that it can be executed using the node
  11. C++——输入、输出和文件
  12. 微信小程序开发BUG经验总结
  13. liquibase-数据库脚本升级管理
  14. 网页制作常用html代码
  15. 将Eclipse设置为中文
  16. 【软考】--软考总结
  17. 我在博客园的这一年小记
  18. 一款非常不错的键盘改建神器
  19. 昆明睿正科技有限公司-创业之初
  20. mysql innodb源代码调试跟踪分析 何登成_Mysql+Innodb源代码调试跟踪分析+何登成_IT168文库...

热门文章

  1. (chap4 IP协议) IP协议
  2. BUUCTF firmware
  3. 通过Jenkins执行脚本,生成自动化测试报告
  4. 03_Introduction_to_AMBA_AXI
  5. C语言用指针求两个矩阵的差,求破C语言用指针和函数求转置矩阵
  6. Parallels高危漏洞的奇葩修复指南
  7. ASP.NET运行环境配置
  8. UNIX环境高级编程第二版_扫描 版笔记
  9. android 开发文档模板
  10. 1.2 实例:封装图书信息类