自定义注解类,模拟JUnit测试类的执行顺序

本文介绍了如何创建五个自定义注解来模拟JUnit测试类的执行流程,通过这些注解可以精确地控制测试用例的顺序,实现更灵活的测试策略。

五个自定义注解类

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//修饰的范围
@Target(ElementType.METHOD)  
//程序运行时是可见的
@Retention(RetentionPolicy.RUNTIME)
public @interface BeforeClass {

}
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//修饰的范围
@Target(ElementType.METHOD)  
//程序运行时是可见的
@Retention(RetentionPolicy.RUNTIME)  
public @interface AfterClass {

}
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//修饰的范围
@Target(ElementType.METHOD)  
//程序运行时是可见的
@Retention(RetentionPolicy.RUNTIME)  
public @interface Before {

}
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//修饰的范围
@Target(ElementType.METHOD)  
//程序运行时是可见的
@Retention(RetentionPolicy.RUNTIME)  
public @interface After {

}
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//修饰的范围
@Target(ElementType.METHOD)  
//程序运行时是可见的
@Retention(RetentionPolicy.RUNTIME)  
public @interface Test {

}

测试类

package test;

import annotation.After;
import annotation.AfterClass;
import annotation.Before;
import annotation.BeforeClass;
import annotation.Test;

public class JUnitTest {
	@BeforeClass
	public void funBeforeClass(String str) {
		System.out.println("类" + str + "开始");
		System.out.println("方法####BeforeClass");
	}

	@BeforeClass
	public void funBeforeClass2(String str) {
		System.out.println("类" + str + "开始");
		System.out.println("方法####BeforeClass");
	}

	@AfterClass
	public void funAfterClass(String str) {
		System.out.println("方法####AfterClass");
		System.out.println("类" + str + "结束");
	}

	@Before
	public void funBefore() {
		System.out.println("方法****Before");
	}

	@After
	public void funAfter() {
		System.out.println("方法****After");
	}

	@Test
	public void drink() {
		System.out.println("方法----drink");
		System.out.println("我要喝果汁");
	}

	@Test
	public void eat() {
		System.out.println("方法----eat");
		System.out.println("我要吃馒头");
	}

	@Test
	public void play() {
		System.out.println("方法----play");
		System.out.println("我要玩电脑");
	}

}

模拟JUnit执行顺序的类

package junit;

import java.lang.reflect.Method;
import java.util.ArrayList;

import annotation.After;
import annotation.AfterClass;
import annotation.Before;
import annotation.BeforeClass;
import annotation.Test;
import test.JUnitTest;

public class JUnitFW {
	public static void main(String[] args) throws Exception {
		core("test.JUnitTest");
	}

	public static void core(String className) throws Exception {
		// BeforeClass方法
		Method methodBeforeClass = null;
		// AfterClass方法
		Method methodAfterClass = null;
		// After方法
		Method methodAfter = null;
		// Before方法
		Method methodBefore = null;

		// Test方法
		ArrayList<Method> testMethods = new ArrayList<Method>();
		// 获取Class对象
		Class<?> jUnitTestClass = Class.forName(className);
		// 对象的实例化
		JUnitTest junitTest = (JUnitTest) jUnitTestClass.newInstance();
		// 取得所有方法
		Method[] methods = jUnitTestClass.getMethods();

		// 取出所有的test方法
		for (Method met : methods) {
			// BeforeClass
			if (met.isAnnotationPresent(BeforeClass.class)) {
				methodBeforeClass = met;
			}
			// AfterClass
			if (met.isAnnotationPresent(AfterClass.class)) {
				methodAfterClass = met;
			}
			// Before
			if (met.isAnnotationPresent(Before.class)) {
				methodBefore = met;
			}
			// After
			if (met.isAnnotationPresent(After.class)) {
				methodAfter = met;
			}
			// Test
			if (met.isAnnotationPresent(Test.class)) {
				testMethods.add(met);
			}
		}
		// BeforeClass
		methodBeforeClass.invoke(junitTest, className);
		System.out.println();
		// Test
		for (Method m : testMethods) {
			// Before
			methodBefore.invoke(junitTest);
			// Test方法的执行
			m.invoke(junitTest);
			// After
			methodAfter.invoke(junitTest);
			System.out.println();
		}
		System.out.println();
		// AfterClass
		methodAfterClass.invoke(junitTest, className);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值