非常实用的Junit3与Junit4 测试 以及两者平滑过渡(高兼容性)实例代码

添加junit3与junit4的jar包的图:

创建放单元测试专用包:

一。业务类:

package com.edu.test;

public class HelloWorld {
  public String hello(){
    return "world";
   }
   public String world(){
    return "hello";
   }
   public String nil(){
    return null;
   }
   public String notNil(){
    return "abc";
   }
   public String ext(){
    throw new NumberFormatException();
   }
}

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

二、junit3测试类(junit3的测试方法必须以test开头,而junit4不需要;junit3必须要继承TestCase,而junit4不需要继承任何类!)

package com;
import com.edu.test.HelloWorld;

import junit.framework.TestCase;
/**
 * junit3 test
 * @author Administrator
 *
 */

public class Junit3TestHelloWord extends TestCase{
 private HelloWorld hw;
 @Override
 protected void setUp() throws Exception {
  super.setUp();
  hw=new HelloWorld();
  System.out.println("测试开始");
 }
 
    public void testHello(){//业务异常测试
     String str=hw.hello();
     assertEquals("hello 测试失败", str, "world");
    }
  
    public void testWorld(){//业务测试
    String str=hw.world();
      assertEquals("world 测试失败", str, "hello");
    }
 
    public void testNil(){//为空测试
     assertNull("对象不为空",hw.nil());
    }
   
    public void testNotNil(){//不为空测试
     assertNotNull("对象为空", hw.notNil());
    }
  
    public void testExt(){//异常测试
     try {
   hw.ext();
   fail("没有抛出异常");//有异常,却没有抛出,说明测试失败
  } catch (NumberFormatException e) {
   e.printStackTrace();
  }
    }
 @Override
 protected void tearDown() throws Exception {
  super.tearDown();
  hw=null;
  System.out.println("测试结束");
 }

}

========================

三、junit4测试类

package com;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.edu.test.HelloWorld;
import static org.junit.Assert.*;//静态导入包中所以内容
/**
 * junit4 test
 * @author Administrator
 *
 */
public class Junit4TestHelloWorld {
 private HelloWorld hw;
 @Before
   public void setUp(){//测试开始
    hw=new HelloWorld();
   }
   @Test
   public void testHello(){//业务异常测试
    String str=hw.hello();
    assertEquals("hello 测试失败", str, "world");
   }
   @Test
   public void testWorld(){//业务测试
   String str=hw.world();
  assertEquals("world 测试失败", str, "hello");
   }
   @Test
   public void testNil(){//为空测试
    assertNull("对象不为空",hw.nil());
   }
   @Test
   public void testNotNil(){//不为空测试
    assertNotNull("对象为空", hw.notNil());
   }
      @Test
   public void junit3TestExt(){//异常测试
    try {
  hw.ext();
 } catch (NumberFormatException e) {
  e.printStackTrace();
 }
 }
  @Test(expected=NumberFormatException.class)//junit4独有的,junit3无法过渡
 public void junit4TestExt(){//异常测试
    hw.ext();
   }  

 @After
   public void tearDown(){//测试结束
   hw=null;
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值