在项目经常会用到单元测试,这里对Junit在开发中的使用标准及使用方法进行简单的介绍。

1.包目录的定义以及相关jar包的添加

2.Junit3和Junit4分别对测试类的编写

所测试的源代码:

  1. package com.techbirds;
  2. public class HelloWorld {
  3. public void sayHello(){
  4. System.out.println("hello....");
  5. throw new NumberFormatException();
  6. }
  7. public void sayWorld(){
  8. System.out.println("world....");
  9. }
  10. public String say(){
  11. return "hello world!";
  12. }
  13. }

Junit3测试类编写:

  1. package com.techbirds;
  2. import junit.framework.TestCase;
  3. public class HelloWorldTest extends TestCase{
  4. private HelloWorld hw;
  5. @Override
  6. protected void setUp() throws Exception {
  7. super.setUp();
  8. hw=new HelloWorld();
  9. }
  10. //1.测试没有返回值
  11. public void testHello(){
  12. try {
  13. hw.sayHello();
  14. } catch (Exception e) {
  15. System.out.println("发生异常.....");
  16. }
  17. }
  18. public void testWorld(){
  19. hw.sayWorld();
  20. }
  21. //2.测试有返回值的方法
  22. // 返回字符串
  23. public void testSay(){
  24. assertEquals("测试失败", hw.say(), "hello world!");
  25. }
  26. //返回对象
  27. public void testObj(){
  28. assertNull("测试对象不为空", null);
  29. assertNotNull("测试对象为空",new String());
  30. }
  31. @Override
  32. protected void tearDown() throws Exception {
  33. super.tearDown();
  34. hw=null;
  35. }
  36. }

Junit4测试类编写:

  1. package com.techbirds;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. //导入Assert类的静态方法-为了便于junit4->junit3的转换
  6. import static org.junit.Assert.*;
  7. public class HelloWorldTest {
  8. private HelloWorld hw;
  9. @Before
  10. public void setUp() {
  11. hw = new HelloWorld();
  12. }
  13. @Test(expected=NumberFormatException.class)
  14. // 1.测试没有返回值,有别于junit3的使用,更加方便
  15. public void testHello() {
  16. hw.sayHello();
  17. }
  18. @Test
  19. public void testWorld() {
  20. hw.sayWorld();
  21. }
  22. @Test
  23. // 2.测试有返回值的方法
  24. // 返回字符串
  25. public void testSay() {
  26. assertEquals("测试失败", hw.say(), "hello world!");
  27. }
  28. @Test
  29. // 返回对象
  30. public void testObj() {
  31. assertNull("测试对象不为空", null);
  32. assertNotNull("测试对象为空", new String());
  33. }
  34. @After
  35. public void tearDown() throws Exception {
  36. hw = null;
  37. }
  38. }

全部测试类运行:

Junit3和Junit4使用区别的更多相关文章

  1. JUnit3 和 JUnit4的区别

    JUnit3 和 JUnit4的区别 1.JUnit 4使用org.junit.*包而JUnit 3.8使用的是junit.Framework.*;为了向后兼容,JUnit4发行版中加入了这两种包. ...

  2. Junit3与Junit4的区别

    Junit4最大的亮点就是引入了注解(annotation),通过解析注解就可以为测试提供相应的信息,抛弃junit3使用命名约束以及反射机制的方法. /** * 被测试类 */ package co ...

  3. junit3和junit4的区别总结

    先来看一个例子: 先用junit3来写测试用例,如下: junit3测试结果: 从上面可看出: 1.junit3必须要继承TestCase类 2.每次执行一个测试用例前,junit3执行一遍setup ...

  4. junit3和junit4的使用区别如下

    junit3和junit4的使用区别如下1.在JUnit3中需要继承TestCase类,但在JUnit4中已经不需要继承TestCase2.在JUnit3中需要覆盖TestCase中的setUp和te ...

  5. junit基础学习之-junit3和4的区别(4)

    junit3和junit4的使用区别如下 1.在JUnit3中需要继承TestCase类,但在JUnit4中已经不需要继承TestCase 2.在JUnit3中需要覆盖TestCase中的setUp和 ...

  6. junit3对比junit4

    本文内容摘自junit实战,感谢作者的无私奉献. 个人觉得每个开源包的版本对比意义不大,闲来无事,这里就来整理一下好了.本文名为junit3对比junit4,但是我通过这篇博客主要也是想统一的来整理下 ...

  7. 《Gradle权威指南》--Android Gradle测试

    No1: Android既可以用传统的JUnit测试,也可以用Android的instrument测试. No2: 当我们运行测试的时候,androidTest SourceSet会被构建成一个可以安 ...

  8. 2016-2017-2 20155325实验二《Java面向对象程序设计》实验报告

    实验二 面向对象程序设计-1 答案截图 码云代码链接 链接1 实验遇到的问题和解决过程 问题1:在plugins里搜索不到JUnitGenerator V2.0只能搜到Generste Teats 问 ...

  9. junit学习之junit的基本介绍

    Junit目前在一些大的公司或者相对规范的软件中使用的比较多,相当多的小公司并没有把单元测试看的太重要.在大点的公司开发人员每天上班后,第一件事情就是从svn上把自己负责的代码checkout下来,然 ...

随机推荐

  1. C# 使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕

    using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.T ...

  2. JavaScript高速掌握

    .document.write(""); 输出语句 .JS中的凝视为//或/* */ .传统的HTML文档顺序是:document->html->(head,body) ...

  3. 通过tarball形式安装HBASE Cluster(CDH5.0.2)——集群安装总览

    1,手动下载压缩包.tar(下载地址),采用tarball形式手工安装集群. 2,共启用13台虚拟机,CentOS6.5 64bit,nn1,nn2,rm1,rm2,dn1,dn2,dn3,dn4,d ...

  4. DIV背景图片定位问题

    <div class="custom-topNavigation_shadow"> </div>   正确写法 .custom-topNavigation_ ...

  5. 【LSTM】Understanding-LSTMs

    http://colah.github.io/posts/2015-08-Understanding-LSTMs/

  6. SpringMVC使用@ResponseBody时返回json的日期格式及可能产生的问题

    http://blog.csdn.net/z69183787/article/details/40375831 遇到的问题: 1 条件: 1.1.表单里有两个时间参数,都是作为隐藏项随表单一起提交: ...

  7. Python爬虫-什么是爬虫?

    百度百科是这样定义爬虫的: 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂 ...

  8. Hibernate session FlushMode的五种设置

    http://www.2cto.com/kf/201207/141455.html Hibernate session FlushMode有五种属性:1.NEVEL:已经废弃了,被MANUAL取代了2 ...

  9. 5种实现垂直居中css

    摘要: 在我们制作页面的时候经常会遇到内容垂直居中的需求,今天分享5种垂直居中的方法,每种方法都有自己的优缺点,可以选择自己喜欢的方式.以下代码都经过本人亲自测试. line-height: < ...

  10. JavaWeb跨域访问问题

    转载: http://blog.csdn.net/zjq_1314520/article/details/65449279 最后的解决方案如下: 在 tomcat 的 conf目录下找到 web.xm ...