在一个测试类中,所有被@Test注解修饰的public,void方法都是testcase,可以被JUNIT执行。

@Retention(value=RUNTIME)
@Target(value=METHOD)
public @interface Test
Test中的属性(可选的):1.expected:如果被期待的指定的异常抛出,则表示成功。
       2.timeout:执行失败的时间
测试的方法名不用以test开头,但规范来写还是以test开头
1.setup方法在3.8中是在每个测试的方法前初始化,在4.0中要采用注解的方式@Before,与tearDown对应的@After

2.4.0中新加入@BeforeClass,@AfterClass.相当于全局初始化和销毁 修饰的方法必须为public static void no-arg.

以Calculator的testAdd测试timeout属性

先让add方法睡眠500ms;

以Calculator的testDivide测试expected属性

 import static org.junit.Assert.assertEquals;

 import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; public class CalculatorTest {
private Calculator cal;
@BeforeClass
public static void globalInit(){
System.out.println("全局初始化");
} @Before
public void init(){
cal=new Calculator();
} @Test(timeout=200)
public void testAdd(){ int result=cal.add(3, 1);
assertEquals(4, result);
} @Test(expected=Exception.class)
public void testDivide() throws Exception{
cal.divide(3, 0);
}
}
@Retention(value=RUNTIME)
@Target(value={METHOD,TYPE})
public @interface Ignore 可以修饰方法也可以修饰类,表示忽略 @RunWith 运行哪个运行器
Parameterized:参数化运行器
参数化测试(Parameters)需要在类的声明处加上@RunWith(Parameterized.class),在提供参数的方法上要使用@Parameters注解修饰,同时在构造方法中为各个参数赋值(构造方法由junit调用),最后编写测试类,它会根据组数来运行测试次数。
 import static org.junit.Assert.*;

 import java.util.Arrays;
import java.util.Collection; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class)
public class ParameterizedTest {
//设定参数 预计值, 参数1 参数2
private int expected;
private int input1;
private int input2;
private Calculator cal; @SuppressWarnings("rawtypes")
@Parameters
public static Collection prepareData(){
Object[][] object={{5,4,1},{8,2,6},{9,4,5}};
//数组转换为集合
return Arrays.asList(object);
} @Before
public void setUp(){
cal=new Calculator();
} public ParameterizedTest(int expected,int input1,int input2){
this.expected=expected;
this.input1=input1;
this.input2=input2;
} @Test
public void testAdd(){
assertEquals(this.expected, cal.add(this.input1, this.input2));
}
}
Suite(运行器)
@RunWith(Suite.class)
@Suite.SuiteClasses({要运行的类(数组)}) 自动化测试
 import org.junit.runner.RunWith;
import org.junit.runners.Suite; @RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorTest.class,LargestTest.class,ParameterizedTest.class})
public class TestAll { }
												

junit4学习(Annotation)的更多相关文章

  1. Junit4学习使用和总结

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

  2. Junit4学习(一)新建Junit4工程

    一,学习Junit4,学以致用 二,熟悉编写流程 工具:Eclipse,Junit包,hamcrest.core包 1,打开Eclipse开发工具,新建工程:file->Java Project ...

  3. JUnit4 学习笔记

    一.环境搭建: 1.需要用的包: JUnit4.7:http://files.cnblogs.com/files/ShawnYang/junit4.7.zip hamcrest-1.2:http:// ...

  4. Junit4学习笔记

    一.初始化标注 在老Junit4提供了setUp()和tearDown(),在每个测试函数调用之前/后都会调用. @Before: Method annotated with @Before exec ...

  5. Junit4学习(六)Junit4参数化设置

    一,背景, 有时候会对相同的代码结构做同样的操作,不同的时对参数的设置数据和预期结果:有没有好的办法提取出来相同的代码,提高代码的可重用度,junit4中使用参数化设置,来处理此种场景: 二,代码展示 ...

  6. 08-spring学习-annotation配置

    利用annotation配置注入关系 为了更好的解释此类存在的意义,下面通过一段习惯性的开发进行问题的描述,例如: 现在有一个IAdminService服务层,这个服务层要调用的是IAdminDAO和 ...

  7. JUnit4学习

    参考:http://www.cnblogs.com/yangxia-test/p/3996120.html JUnit4是一个开源的java单元测试框架,我们只需要引入一个包,就可以使用它的功能 先说 ...

  8. Junit4学习笔记--方法的执行顺序

    package com.lt.Demo.TestDemo; import java.util.Arrays; import java.util.Collection; import org.junit ...

  9. Junit4学习(五)Junit4测试套件

    一,背景 1,随着开发规模的深入和扩大,项目或越来越大,相应的我们的测试类也会越来越多:那么就带来一个问题,假如测试类很多,就需要多次运行,造成测试的成本增加:此时就可以使用junit批量运行测试类的 ...

随机推荐

  1. XmlElement可以避免由XmlSerializer多余生成的代码

    public class Program { static void Main(string[] args) { var alarm = new Alarm() { Code = "1588 ...

  2. Python [Leetcode 350]Intersection of Two Arrays II

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  3. python练习程序(c100经典例21)

    题目: 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上想再吃时,见只 ...

  4. Eclipse实用快捷键

    经典常用快捷键1. [ALT+/]此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2. ...

  5. jboss集成eclipse

    eclipse Kepler + Jboss7.1 参考引用文档: http://www.tekdigest.com/how-to-install-jboss-tools-in-eclipse.htm ...

  6. 【转】bzero, memset ,setmem 区别

    原文网址:http://blog.csdn.net/agathe/article/details/6066157 bzero  原型: extern void bzero(void *s, int n ...

  7. Word2003使用VBA教程

    [正文] 注:本文中所有vba代码都是储存在doc中,而非normal.dot 1.打开一个.doc文档 2.按ALT+F11 3.左侧 Project-插入-模块 4.输入自己的代码,一定要是函数的 ...

  8. 看来ms sql server if 中定义个变量出了if 还是可以用的

    begin declare @abc int; end print @abc 可以打出1出来

  9. web.xml文件的作用

    每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗? 一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. ...

  10. 如何配置Drupal数据库信息?

    Drupal的数据库连接信息通过文件settings.php中的变量$databases设置.变量$databases是一个二维的数组,第一维称为key,第二维称为target.使用这种方式可以处理多 ...