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

被折叠的 条评论
为什么被折叠?



