在一个测试类中,所有被@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. 20160207.CCPP体系详解(0017天)

    程序片段:01.Point.c+02.进程.c+03.内存的栈和堆.c 内容概要:内存32位和64位的区别 ///01.Point.c #include <stdio.h> #includ ...

  2. 关于jsp利用EL和struts2标签来遍历ValueStack的东东 ------> List<Map<K,V>> 以及 Map<K,<List<xxx>>> 的结构遍历

    //第一种结构Map<K,<List<xxx>>> <body> <% //显示map<String,List<Object>& ...

  3. dede 去power by dedecms

    include/dedesql.class.php 下的$arrs1和$arrs2的全注释掉

  4. Mac设置截图保存位置

    补充: killall 用来杀死指定名字的进程 defaults 可以对一些系统属性进行read,write,delete操作 下面举几个常用的例子: 1.显示隐藏文件 defaults write ...

  5. MYSQL中delete删除多表数据

    MYSQL中delete删除多表数据 DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 1. delete from t1 where 条件 2.delete t1 ...

  6. yii 操作session和cookie

    一,在Yii中使用session 1,CHttpSession 与原生态php5的session使用差别是,php5使用session_start();$_session['key'] = $valu ...

  7. int (*p)[4] p 是二级指针 二维数组 二级指针 .xml

    pre{ line-height:1; color:#2f88e4; background-color:#e9ffff; font-size:16px;}.sysFunc{color:#3d7477; ...

  8. bzoj3064 CPU监控

    今天终于写了一道正常的题 思路是这样的: 1.普通线段树add,set不变,并改为下放标记版本 2.past_addv 记录一个区间内可能的addv值的最大值 3.past_setv 记录一个区间被s ...

  9. ajax 访问--提高安全性

    首先受到struts token的启发,产生了客户端发起的ajax请求进行验证的想法,大致思路是客户端每次请求产生一个key ,然后服务端接收到key,然后解析,判断是否为合法key, 对于不带key ...

  10. 在Chrome Console中加载jQuery

    var jq = document.createElement('script'); jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1/j ...