反射应用单体测试例子

采用反射的形式,进行类的单体测试
文件目录结构

com.Main
       Main.java
       Screen.java
com.UnitTest
       ScreenTest.java

Main.java

在Main.java中直接使用反射的方式,调用Screen.java的接口,在ScreenTest中采用单体测试反射调用Screen

package com.Main;
import java.lang.reflect.*;
import com.Main.*;
public class Main {
	
	public static void main(String[] str) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, ClassNotFoundException, NoSuchMethodException, SecurityException {	
		//1
		Screen screen1 =new Screen();
		Class<?> clazz = screen1.getClass();
		//2
		Class<?> clazz1 = Screen.class;
		//3
		Class<?> clazz2 = Class.forName("com.Main.Screen");
		Screen s = (Screen)clazz2.newInstance();
		s.fun();
		//调用无参数
		try {
			System.out.println("-----------------调用无参");
			Class<?> cls = Screen.class;
			Method method = cls.getDeclaredMethod("fun", null);
			method.setAccessible(true);
			method.invoke(cls.newInstance());
			method.setAccessible(false);
		}catch(Exception e) {
			System.out.println(e.getStackTrace());
		}
		//调用有参
		try {
			System.out.println("-----------------调用有参");
			Class<?> cls = Screen.class;
			Class<?> []args = new Class[2];
			args[0]=String.class;
			args[1]=int.class;
			Method method = cls.getDeclaredMethod("fun1",args);//Method method = cls.getDeclaredMethod("fun1",String.class,int.class);
			method.setAccessible(true);
			method.invoke(cls.newInstance(),"123",123);
			method.setAccessible(false);
		}catch(Exception e) {
			System.out.println(e.getStackTrace());
		}
		//调用内部类
		try {
			System.out.println("-----------------调用内部类");
			Class<?> cls = Screen.class;
			Screen screen = (Screen)cls.newInstance();
			Class<?> cls1 = Class.forName("com.Main.Screen$Bluetooth");
			Constructor<?>[] constructor=cls1.getDeclaredConstructors();
			for(Constructor<?> con:constructor) {
				System.out.println(con);
			}
			constructor[0].setAccessible(true);
			Object obj = constructor[0].newInstance(screen);
			Method method = cls1.getDeclaredMethod("onChange",String.class,int.class);
			method.setAccessible(true);
			method.invoke(obj,"String",123);
			method.setAccessible(false);
			constructor[0].setAccessible(false);
		}catch(Exception e) {
			System.out.print(e.getStackTrace());
		}
	}

}

Screen.java

package com.Main;

public class Screen {
	private static Screen screen;
	public Screen() {
		
	}
	public Screen(int _a) {
		
	}
	//单例模式
	public static Screen getInstance() {
		if(screen==null) {
			screen = new Screen();
		}
		return screen;
	}
	public void fun() {
		System.out.println("fun() ");
	}
	public void fun1(String _string,int _num) {
		if("hello".equals(_string) && _num == 5) {
			System.out.println("fun1() "+_string+_num);
		}else {
			
		}	System.out.println("fun1() 没有走到的分支");
	}
	
	
	public class Bluetooth {
		public Bluetooth() {
			System.out.println(" Bluetooth()");
		}
		public Bluetooth(String _string) {
			System.out.println("Bluetooth(String _string)");
		}
		public void onChange(String string,int i) {
			System.out.println("Bluetooth类  onChange"+string+" "+i);
		}
	}
}

ScreenTest.java

package com.UnitTest;
import com.Main.*;
import com.Main.Screen.Bluetooth;

import org.junit.*;
import java.lang.reflect.*;
public class ScreenTest {
	
	private static Screen screen;
	
	
	public  Method getFatherMethod(Class<?> clazz,String methodName,final Class<?>[] classes) throws Exception{
		Method method=null;
		try {
			method=clazz.getDeclaredMethod(methodName, classes);
		}catch(NoSuchMethodException e) {
			if(clazz.getSuperclass()==null) {
				return method;
			}else {
				method = getFatherMethod(clazz.getSuperclass(),methodName,classes);
			}
			System.out.println("NoSuchMethodException"+e.getStackTrace());
		}
		return method;
	}
	public  Field getFatherField(Class<?> clazz,String methodName) throws Exception{
		Field field = null;
		try {
			field = clazz.getDeclaredField(methodName);
		}catch(NoSuchFieldException  e) {
			if(clazz.getSuperclass()==null) {
				return field;
			}else {
				field = getFatherField(clazz.getSuperclass(),methodName);
			}
			System.out.println("NoSuchFieldException"+e.getStackTrace());
		}
		return field;
	}
	
	@BeforeClass
	public static void BefoClass() {
		screen = Screen.getInstance();
	}
	@AfterClass
	public static void AfterClass() {
		System.gc();
	}
	@Test
	public void funTest() throws Exception {
		System.out.println("------------------1----begin---------------------");
		try {
			Method method = getFatherMethod(Screen.class,"fun", null);
			if(method!=null) {
				method.setAccessible(true);
				Object invoke = method.invoke(screen);
				if(invoke!=null) {
					System.out.println("invoke  sucess");
				}
				method.setAccessible(false);
			}
		}catch(Exception e) {
			System.out.println("funTest"+e.getStackTrace());
		}
		System.out.println("------------------1----end---------------------");
	}
	@Test
	public void fun1Test() throws Exception{
		System.out.println("------------------2----begin---------------------");
		try {
			Class<?>[] args1 = new Class[2];
			args1[0]=String.class;
			args1[1]=int.class;
			Method method = getFatherMethod(Screen.class,"fun1", args1);
			method.setAccessible(true);
			//method.invoke(screen, null,0);
			method.invoke(screen, "hello",5);
			method.setAccessible(false);			
		}catch(Exception e) {
			System.out.println("fun1Test "+e.getStackTrace());
		}
		System.out.println("------------------2----end---------------------");
	}
	
	@Test
	public void BluetoothTest() {
		System.out.println("------------------3----begin---------------------");
		try {
			Class<?> cls = Class.forName("com.Main.Screen$Bluetooth");
			if(cls != null) {
				System.out.println("------------------cls != null---------------------");
				Class<?>[] args= new Class[2];
				args[0]=String.class;
				args[1]=int.class;
				Method method = cls.getDeclaredMethod("onChange", args);
				
				method.setAccessible(true);
				Constructor<?>[]  constructor = cls.getDeclaredConstructors();
				for(Constructor<?> con:constructor) {
					System.out.println("Constructor<?>  "+con);
				}
				constructor[0].setAccessible(true);
				Object obj = constructor[0].newInstance(screen);//传一个外部类的对象进去		
				method.invoke(obj, "123",123);
				constructor[0].setAccessible(false);		
				method.setAccessible(false);
				
			}
		}catch(Exception e) {
			System.out.println("BluetoothTest "+e.getStackTrace());
		}
		System.out.println("------------------3----end---------------------");
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值