Junit3和Junit4测试包使用区别

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

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

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

所测试的源代码:

  1. package com.techbirds;  
  2.    
  3.  public class HelloWorld {  
  4.       
  5.     public void sayHello(){  
  6.         System.out.println("hello....");  
  7.         throw new NumberFormatException();  
  8.     }  
  9.       
  10.     public void sayWorld(){  
  11.         System.out.println("world....");  
  12.     }  
  13.       
  14.     public String say(){  
  15.         return "hello world!";  
  16.     }  
  17.       
  18.  }  
  19.    
package com.techbirds;
 
 public class HelloWorld {
 	
 	public void sayHello(){
 		System.out.println("hello....");
 		throw new NumberFormatException();
 	}
 	
 	public void sayWorld(){
 		System.out.println("world....");
 	}
 	
 	public String say(){
 		return "hello world!";
 	}
 	
 }
 
Junit3测试类编写:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值