模拟junit4单元测试

在这里插入图片描述

模拟junit4.x,先得明白junit4.x的运行效果:
先执行@Before标注的方法,再运行@Test标注的方法,最后运行@After标注的方法.
思路:
1:开发出3个注解
2:把注解贴在测试类中
3:开发第三方程序赋予注解功能(执行的先后顺序)
3.1:获取到要测试的字节码对象
3.2:获取字节码对象中的所有方法
3.3:归类区分带有不同注解的方法,分3类
3.4:迭代所有要执行的方法
3.4.1在所有要执行的方法前先执行MyBefore注解的方法
3.4.2在所有要执行的方法后再执行MyAfter注解的方法

自定义After注解

package com.xulihao.zhujie._test;

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



@Retention(RetentionPolicy.RUNTIME)//元注解设置注解执行到哪一步 source class runtime
@Target(ElementType.METHOD)//设置执行的范围
public @interface After {

}

自定义Before注解

package com.xulihao.zhujie._test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Before {

}

自定义Test注解

package com.xulihao.zhujie._test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {

}

测试类Myunitetest

public class Myunitetest {
	@Before
	public void init() {
		System.out.println("init。。。。。。。。");
	}
	@After
	public void end() {
		System.out.println("end.........");
	}
	@Test
	public void test1() {
		System.out.println("方法一运行中");
	}
	@Test
	public void test2() {
		System.out.println("方法二运行中");
	}
}

开发第三方程序App类赋予注解功能(执行的先后顺序)

package com.xulihao.zhujie._test;

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

public class App {
/*
 * 
 模拟junit4.x,先得明白junit4.x的运行效果:
  先执行@Before标注的方法,再运行@Test标注的方法,最后运行@After标注的方法.
----------------------
思路:
1:开发出3个注解
2:把注解贴在测试类中
3:开发第三方程序赋予注解功能(执行的先后顺序)
  3.1:获取到要测试的字节码对象
  3.2:获取字节码对象中的所有方法
  3.3:归类区分带有不同注解的方法,分3类
  3.4:迭代所有要执行的方法
        在所有要执行的方法前先执行MyBefore注解的方法
        在所有要执行的方法后再执行MyAfter注解的方法
 */
	public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
		Method befo=null;
		Method aft=null;
		List<Method> methodlist=new ArrayList<>();
		//获取测试的字节码对象
		Class<?> clz=Myunitetest.class;
		//Class<?> clz=Class.forname();
		Object obj=clz.newInstance();//反射创建对象
		Method[] meth=clz.getMethods();//得到类的方法对象并存到method数组中
		for (Method me : meth) {//归类区分
			if(me.isAnnotationPresent(Before.class)) {//根据注解判断方法
				befo=me;
			}else if(me.isAnnotationPresent(After.class)) {
				aft=me;
			}else if(me.isAnnotationPresent(Test.class)){
				methodlist.add(me);
			}
		}
		for (Method m : methodlist) {//迭代要执行的方法
			if(befo!=null) {//在执行之前,首先得判断这个方法是否存在
				befo.invoke(obj);//invoke后要传当当前类的对象
			}
			m.invoke(obj);//通过反射调用方法
			if(aft!=null) {
				aft.invoke(obj);
			}
		}
		/*
		System.out.println(befo);
		System.out.println(aft);
		System.out.println(methodlist);
		*/
	}
}

执行效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42405666/article/details/89790352