package com.passer.junit4;
/*
* JUnit测试单元
*/
public class JUnit4{
@MyBefore
public void before() throws Exception{
System.out.println("before...");
}
@MyAfter
public void after() throws Exception{
System.out.println("after...");
}
@MyTest
public void test1() throws Exception{
System.out.println("测试1");
}
@MyTest
public void test2() throws Exception{
System.out.println("测试2");
}
}
package com.passer.junit4;
/*
* 模拟JUnit中@Before的注解
*/
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(METHOD)
public @interface MyBefore {
}
package com.passer.junit4;
/*
* 模拟JUnit中的@After注解
*/
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(METHOD)
public @interface MyAfter {
}
package com.passer.junit4;
/*
* 模拟JUnit中的@Test注解
*/
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(METHOD)
public @interface MyTest {
}
package com.passer.junit4;
import java.lang.reflect.Method;
import java.util.ArrayList;
/*
* 模拟JUnit
*/
public class JUnitMock {
public static void main(String []args) throws Exception {
//获取JUnit4的字节码对象
Class<JUnit4> clz=JUnit4.class;
//获取JUnit4的实例对象
JUnit4 junit4=clz.newInstance();
//获取出JUnit4的所有方法
Method [] ms=clz.getDeclaredMethods();
//迭代出每一个方法,并判断每一个方法分别使用了什么注解,并归类存储
ArrayList<Method> beforeList=new ArrayList<>();
ArrayList<Method> testList=new ArrayList<>();
ArrayList<Method> afterList=new ArrayList<>();
for (Method m : ms) {
if(m.isAnnotationPresent(MyBefore.class)) {
beforeList.add(m);continue;
}else if(m.isAnnotationPresent(MyTest.class)){
testList.add(m);
}else if(m.isAnnotationPresent(MyAfter.class)) {
afterList.add(m);
}
}
//循环迭代出testList中的每一个测试方法,并执行
for (Method method : testList) {
for(Method m:beforeList) {
m.invoke(junit4);
}
method.invoke(junit4);
for (Method m : afterList) {
m.invoke(junit4);
}
}
}
}