模拟Junit4.0

junit4.x的特点:

   存在:Before,Test,After三个标签.

   执行特点:        Before标注方法--->Test标注方法-->After标注的方法

 

二 使用步骤

1 添加依赖库 选中项目 ->Build Path->Add Libraries->选择版本为4.0

会在项目中添加一个Junit4的依赖包

2 创建一个测试类

 

package com.OSA.Day01.Junit5_04;

public class EmployeeDAOTest {

	//测试保存
	public void testSave() throws Exception {
		System.out.println("...save...");
	}
	
	//测试删除
	public void testDelete() throws Exception {
		System.out.println("...delete...");
	}
	
	//测试更新
	public void testUpdate() throws Exception {
		System.out.println("...update...");
	}

}


在outline方法处选中一个方法,右击runAs会提醒配置文件

现在给第一个方法上添加一个标签@Test,重复上述方法,就会出现一个JUnit Test

 

 

 

 

点击之后会出现运行时间和运行该方法,脱离main方法,直接运行方法

3 添加一个init 一个destory方法,在上面添加@Before @After

 

package com.OSA.Day01.Junit5_04;

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

public class EmployeeDAOTest {

	
	@Before
	public void init(){
		System.out.println("初始化...");
	}
	
	@After

	public void destory(){
		System.out.println("销毁...");
	}
	
	
	@Test
	//测试保存
	public void testSave() throws Exception {
		System.out.println("...save...");
	}
	
	@Test
	//测试删除
	public void testDelete() throws Exception {
		System.out.println("...delete...");
	}
	@Test
	//测试更新
	public void testUpdate() throws Exception {
		System.out.println("...update...");
	}

}


此时点击整个项目,run ad jutil


可以看出没执行一个测试方法,都会在之前加上初始化和销毁的方法

 

 

三 模拟该方法

 

 

操作步骤:
    1):先找到测试类的字节码:EmployeeDAOTest
    2):获取EmployeeDAOTest类中所有的公共方法.
    3):迭代出每一个Method对象.
    4):边迭代边判断,哪一些方法使用了@Before/@Test/@After标签标注.    
             beforeList:  存储使用了@Before标签标注的方法对象.
             testList:       存储使用了@Test标签标注的方法对象.
             afterList:     存储使用了@After标签标注的方法对象.
     5):控制方法执行的流程.
                执行beforeList中的方法
          迭代出testList集合中的每一个方法对象,并执行.
                执行afterList中的方法


 

 

 

注解在现在的开发中运用非常广泛:

   简单,简洁--->!

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

之前在XML文件中配置8行的Servlet,只需要一个@WebServlet(“/XX”)就搞定.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值