public static void main(String[] args) {
stdOut.println("");
stdOut.println("Testing class Movie...");
//Movie movie;
Class classDescriptor = null;
try {
classDescriptor = Class.forName("Movie");//查找Movie类,且赋给变量classDescriptor
} catch (ClassNotFoundException cnfe) {
fail("1: Class Movie not found " +
"(check the name of the class)");
}
assertTrue("2: Class Movie is not abstract",
Modifier.isAbstract(classDescriptor.getModifiers()));//检测是否是抽象类
Constructor[] constructors = classDescriptor.getConstructors();//获取构造函数
assertTrue("3: Movie should contain one public constructor",
constructors.length == 1);
Class[] parameterTypes = constructors[0].getParameterTypes();//获取构造函数的参数类型
assertTrue(
"4: the number of parameter of the constructor is incorrect",
parameterTypes.length == 3);
assertTrue("5: the parameters of the constructor are incorrect ",
parameterTypes[0].getName().equals("java.lang.String") &&
parameterTypes[1].getName().equals("[Ljava.lang.String;") &&//注意string数组的表示
parameterTypes[2].getName().equals("java.lang.String"));//检测参数类型是否一致
Method[] methods = classDescriptor.getDeclaredMethods();//获取方法
assertTrue("6: Movie should contain three methods",
methods.length == 4);
try {
Method method = classDescriptor.getMethod("getTitle", new Class[0]);//获取方法
Class returnType = method.getReturnType();//获取方法的返回类型
assertTrue("7: the return type of method getTitle is incorrect",
returnType.getName().equals("java.lang.String"));
} catch (NoSuchMethodException nsme) {
fail("5: Method getTitle not found " +
"(check the name of the method)");
}
try {
Method method =
classDescriptor.getMethod("getActors", new Class[0]);
Class returnType = method.getReturnType();
assertTrue("8: the return type of method getActors is incorrect",
returnType.getName().equals("[Ljava.lang.String;"));
} catch (NoSuchMethodException nsme) {
fail("9: Method getActors not found " +
"(check the name of the method)");
}
try {
Method method =
classDescriptor.getMethod("getDirector", new Class[0]);
Class returnType = method.getReturnType();
assertTrue("10: the return type of method getDirector is incorrect",
returnType.getName().equals("java.lang.String"));
} catch (NoSuchMethodException nsme) {
fail("11: Method getDirector not found " +
"(check the name of the method)");
}
try {
Method method = classDescriptor.getMethod("toString", new Class[0]);
Class returnType = method.getReturnType();
assertTrue("12: the return type of method toString is incorrect",
returnType.getName().equals("java.lang.String"));
} catch (NoSuchMethodException nsme) {
fail("13: Method toString not found " +
"(check the name of the method)");
}
// Testing constructor
Constructor[] arrayConstructors = classDescriptor.getConstructors();//获取构造函数
assertTrue("14: the class should contain one constructor",
arrayConstructors.length == 1);
Constructor constructor = arrayConstructors[0];
Class[] arrayParameters = constructor.getParameterTypes();//获取构造函数参数类型
assertTrue("15: the constructor should contain three parameters",
arrayParameters.length == 3);
assertTrue("16: the types of the parameters in the constructor are "
+ "incorrect",
arrayParameters[0].getName().equals("java.lang.String") &&
arrayParameters[1].getName().equals("[Ljava.lang.String;") &&
arrayParameters[2].getName().equals("java.lang.String"));//检测参数类型是否正确
stdOut.println("done");
}
测试abstract class
最新推荐文章于 2023-07-24 14:06:36 发布