注解学习:实现简单的junit的@test注解
2017-05-09 16:27
323 查看
编写注解:
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 Test2 {
}
编写使用注解的类,其中m3,m7抛出异常,m1,m5不抛出异常,但m5为实例方法,不存在注解的有效使用:
public class Sample {
@Test2
public static void m1(){};
public static void m2(){};
编写测试类,利用反射运行注解类中所有还有test2注解的方法:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class RunTest {
public static void main(String[] args) throws Exception {
int tests=0;
int passed=0;
Class testClass=Class.forName(Sample.class.getName());
for(Method m:testClass.getDeclaredMethods()){
if(m.isAnnotationPresent(Test2.class)){
tests++;
try{
m.invoke(null);
passed++;
}catch(InvocationTargetException e){
Throwable exc=e.getCause();
System.out.println(m+"failed"+exc);
}catch (Exception e) {
System.out.println("INVALID @TEST2 :" +m);
}
}
}
System.out.println("tests:"+tests);
System.out.println("passed:"+passed);
}
}
运行后的结果为:
public static void springMVC.Sample.m3()failedjava.lang.RuntimeException: Boom
INVALID @TEST2 :public void springMVC.Sample.m5()
public static void springMVC.Sample.m7()failedjava.lang.RuntimeException: crash
tests:4
passed:1
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 Test2 {
}
编写使用注解的类,其中m3,m7抛出异常,m1,m5不抛出异常,但m5为实例方法,不存在注解的有效使用:
public class Sample {
@Test2
public static void m1(){};
public static void m2(){};
@Test2 public static void m3(){ throw new RuntimeException("Boom"); }; public static void m4(){}; @Test2 public void m5(){}; public static void m6(){}; @Test2 public static void m7(){ throw new RuntimeException("crash"); }; public static void m8(){}; }
编写测试类,利用反射运行注解类中所有还有test2注解的方法:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class RunTest {
public static void main(String[] args) throws Exception {
int tests=0;
int passed=0;
Class testClass=Class.forName(Sample.class.getName());
for(Method m:testClass.getDeclaredMethods()){
if(m.isAnnotationPresent(Test2.class)){
tests++;
try{
m.invoke(null);
passed++;
}catch(InvocationTargetException e){
Throwable exc=e.getCause();
System.out.println(m+"failed"+exc);
}catch (Exception e) {
System.out.println("INVALID @TEST2 :" +m);
}
}
}
System.out.println("tests:"+tests);
System.out.println("passed:"+passed);
}
}
运行后的结果为:
public static void springMVC.Sample.m3()failedjava.lang.RuntimeException: Boom
INVALID @TEST2 :public void springMVC.Sample.m5()
public static void springMVC.Sample.m7()failedjava.lang.RuntimeException: crash
tests:4
passed:1
相关文章推荐
- Java注解三 JUnit注解的简单实现
- AOP/CGLIB学习:实现简单的注解权限系统(Annotation+拦截器)
- 自定义java注解(三) 实现Junit中的@Test、@Before、@After
- map容器的初步学习 -实现简单映射
- Emit学习-实战篇-实现一个简单的AOP框架(二)
- 纯CSS+DIV实现的竖向菜单(简单例子,提供学CSS的朋友学习)
- 小佳弱弱学习——queue队列的简单实现
- ORM,ASP.NET中ORM学习,ASP.NET中ORM学习心得,WEB2.0中ORM实现原理,Asp.net简单ORM示例源码详细讲解,Asp.net2.0:如何使用ObjectDataSource(配合ORM )(二)
- 简单图形编程的学习(1)---文字 (Qt实现)
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- Emit学习-实战篇-实现一个简单的AOP框架(一)
- 简单图形编程的学习(1)---文字 (small basic实现)
- J2ME 3D学习笔记——实现简单的界面(附代码)
- 简单图形编程的学习(1)---文字 (Windows GDI实现)
- 简单图形编程的学习(2)---点 (small basic实现)
- Emit学习-实战篇-实现一个简单的AOP框架(三)
- ORM,ASP.NET中ORM学习,ASP.NET中ORM学习心得,WEB2.0中ORM实现原理,Asp.net简单ORM示例源码详细讲解,Asp.net2.0:如何使用ObjectDataSource(配合ORM )
- 关于java打印功能的最简单实现的学习笔记
- 小佳弱弱学习——tree二叉树的简单实现
- 简单callback实现 注解