What is Unit Test

写了个类,要给别人用,会不会有bug?怎么办?测试一下。

用main方法测试好不好?不好!

1.      不能一起运行!

2.      大多数情况下需要人为的观察输出确定是否正确

Why Unit Test

重用测试,应付将来的实现的变化。

提高士气,明确知道我的东西是没问题的。

软件后期维护成本最高,前期确保健壮性。

JUnit4 HelloWorld

1.      new project---new JUnit TestCase(ClassNameTest    )Eclipse itself hasthe JUnit jar.

With “import static” can use the static test methodsdirectly.

Assert 判断

TestMethod---right click---run as JUnit Test

Keeps the bar green to keeps the code clean

2.      建立类

3.      建立testcase

放弃旧的断言,使用hamcrest断言

1.      assertThat .replace of all theassert method.

Matcher规则匹配 is(expected value)

2.      使用hamcrest的匹配方法 hamcrest jar need to download

a)      更自然 more close to the english

b)      add jar : build path – addexternal archieves

hamcrest-core and hamcrest-library jar.

c)      Class loader error: Remove fromlibrary Eclipse JUnit jar and add the newest JUnit jar

Add external archives

3.      示例

a)     assertThat( n, allOf( greaterThan(1), lessThan(15) )); //  1<n<15
assertThat( n, anyOf( greaterThan(16), lessThan(8) ) );// n>16 | n<8
assertThat( n, anything() );
assertThat( str, is( "bjsxt" ) );//str =
assertThat( str, not( "bjxxt" ) e

b)     assertThat( str, containsString( "bjsxt" ));
assertThat( str, endsWith("bjsxt" ) );
assertThat( str, startsWith( "bjsxt" ) );
assertThat( n, equalTo( nExpected ) );
assertThat( str, equalToIgnoringCase( "bjsxt" ) );
assertThat( str, equalToIgnoringWhiteSpace( "bjsxt" ) );

c)     assertThat( d, closeTo( 3.0, 0.3 ) ); // 3+- 0.3
assertThat( d, greaterThan(3.0) );
assertThat( d, lessThan (10.0) );
assertThat( d, greaterThanOrEqualTo (5.0) );
assertThat( d, lessThanOrEqualTo (16.0) );

d)   Map test

assertThat(map, hasEntry( "bjsxt", "bjsxt" ) );
assertThat( iterable, hasItem ( "bjsxt" ) );
assertThat( map, hasKey ( "bjsxt" ) );
assertThat( map, hasValue ( "bjsxt" ) );

Failure和Error

1.      Failure是指测试失败

2.      Error是指测试程序本身出错 eg: int a = 8/0;

JUnit4 Annotation

1.      @Test: 测试方法

a)      (expected=XXException.class)expect an exception

b)      (timeout=xxx) expect the methodfinish in xxx ms.

2.      @Ignore: 被忽略的测试方法.don’t run the test Methodnow.

3.      @Before: 每一个测试方法之前运行: something to be aprerequisite for the method run

4.      @After: 每一个测试方法之后运行

5.      @BeforeClass: 所有测试开始之前运行 public static void beforeClass()

Only the static method can run before initialize

Function:request for some source or create the APP environment.

Eg:connect to the DB. Configure file.etc.

6.      @AfterClass: 所有测试结束之后运行

Function:to set free some source or over the APP environment.

Eg: disconnect to the DB.etc.

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Test;import com.bjsxt.junit4.T;public class TTest {@BeforeClasspublic static void beforeClass() {System.out.println("beforeClass");}@AfterClasspublic static void afterClass() {System.out.println("afterClass");}@Beforepublic void before() {System.out.println("before");}@Testpublic void testAdd() {int z = new T().add(5, 3);assertThat(z, is(8));assertThat(z ,allOf(greaterThan(5), lessThan(10)));//int a = 8/0;}@Test(expected=java.lang.ArithmeticException.class, timeout=100)public void testDivide() {int z = new T().divide(8, 0);}@Afterpublic void after() {System.out.println("after");}}

Run multi tests

right click on the packageName>runconfiguration>select “run all tests ….”

Conclusion Note:

TTD: test driven development. Put forward a important idea: write the testsfirsty.

JUnit can testJDBC,HTML,Struts,Hibernate,Spring,EJB,but need other jars.

1.      遵守约定,比如:

a)      类放在test包中

b)      类名用XXXTest结尾

c)      方法用testMethod命名

其他框架

TestNG

Ant: A very big project that need it tobuild ,deploy,compile,test. Autocally.

Buteclipse can’t do it .but use Unix shell and Ant can realize the automation.

Custom the build.xml

JUnit4 Note (尚学堂马士兵)相关推荐

  1. 尚学堂-马士兵-专题-正则表达式

    一.最简单的正则表达式 String类的一个方法matches. package com.string;public class RegexTest {public static void main( ...

  2. 马士兵 java 视频

    <尚学堂马士兵 Java 视频集锦(全)>最新版[压缩包]_ed2kers电驴资源下载网站_让分享继续 http://www.ed2kers.com/教育/计算机/309657.html

  3. 尚学堂学习笔记。。。

    1.02_尚学堂马士兵_Struts2_Struts2_HelloWorld_2.avi 指定Tomcat的目录,指定JDK搭建开发环境(拷贝jar包,复制struts.xml文件 此文件不要放在WE ...

  4. 尚学堂轻松愉快学Linux视频教程

    尚学堂马士兵轻松愉快学Linux视频教程下载.简单地说,Linux是一套免费使用和自由传播的类Unix操作系统,它主要用于基于Intel x86系列CPU的计算机上.这个系统是由世界各地的成千上万的程 ...

  5. 尚学堂Java培训:JAVA优秀书籍推荐

    转自:[http://www.bjsxt.com/books/goodbooks.html] 如果你曾经尝试过自学某些知识点,比如JavaSE.JDBC等等,相信有很多情况会觉得按照书上的操作非常难进 ...

  6. [原创 - 尚学堂科技 - 马士兵老师]

    JAVA自学之路 一:学会选择 [转载请注明出处:http://www.bjsxt.com/zixue/zixuezhilu_1.html] 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多 ...

  7. [转]尚学堂科技 - 马士兵老师-JAVA自学之路

    [原创 - 尚学堂科技 - 马士兵老师] JAVA自学之路 一:学会选择 [转载请注明出处:http://www.bjsxt.com/zixue/zixuezhilu_1.html] 为了就业,不少同 ...

  8. JAVA自学之路 [原创 - 尚学堂科技 - 马士兵老师]

    (我觉得看了之后挺不错的所以分享一下) JAVA自学之路 一:学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3 ...

  9. 马士兵java视频学习顺序

    第一部分:J2se学习视频内容包括: 尚学堂科技_马士兵_JAVA视频教程_JDK5.0_下载-安装-配置 尚学堂科技_马士兵_JAVA视频教程_J2SE_5.0_第01章_JAVA简介_源代码_及重 ...

  10. 尚学堂java培训_送给 Java 自学者或者初学者的最全知识清单,2020 年 Java 就该这么学...

    最近逛知乎,发现有很多想自学 Java 或者 Java 初学者提问,不知道如何学习 Java?我接触 Java 快 8 年的时间了,一直从事 Java 开发工作,自己一直升级打怪,对于如何更好的学习 ...

最新文章

  1. rocketmq 顺序消费_必须先理解的RocketMQ入门手册,才能再次深入解读
  2. CentOS7 Python3安装redis
  3. POJ 3468 A Simple Problem with Integers (1)
  4. 光模块、连接器、光纤的常用知识
  5. oracle 修改nls_characterset,ORACLE NLS_CHARACTERSET字符集的更改
  6. linux 权限提示信息,命令行快速提示:权限进阶 | Linux 中国
  7. 织梦 tags.php静态化,dedecms网站tag标签全部静态化的解决方法
  8. 小米12 Ultra将搭载5倍潜望镜头:自研技术加持 成像相对更好
  9. [妙味DOM]第一课:DOM基础概念、操作
  10. 什么录播软件好用?超级好用的录屏软件在这里
  11. IDEA搭建SpringMVC+Spring+Mybatis项目
  12. react native 抖音视频列表页
  13. python 实现复制文件夹以及文件夹下的子文件
  14. godaddy 服务器位置,Godaddy DNS服务器列表
  15. Coded UI Test 常见问题总结
  16. 计算机专业的学生也太太太太太惨了吧?
  17. Android萤石云视频缩放
  18. springboot+老年康复中心信息管理系统 毕业设计-附源码250859
  19. python any函数_python中的any函数是什么?如何使用any函数?
  20. 洛谷P4188 Lifeguards S

热门文章

  1. 奈奎斯特曲线怎么确定w的值matlab,用MATLAB绘制Nyquist图.ppt
  2. 数据同步工具简单介绍
  3. win10录屏_一分钟教你学会两种电脑录屏的方法,以后别再说不知道了
  4. 安装Windowsxp虚拟机
  5. iOS-OC-修改微信运动步数(读取和修改健康步数HealthKit)
  6. 【Excel】两组行数不同数据做二维柱状图
  7. AS3中将TUIO协议转换到传统触摸事件
  8. TwinCAT 3 使用XML-server
  9. 微生物组数据系统发育分析的方法
  10. 数据结构折半查找例题_查找(习题课)