译自:http://junit.sourceforge.net/doc/cookstour/cookstour.htm
不是直接翻译。
1. Command 模式
一个TestCase就是一个Command,command内容都写在run方法里。
2. A best practice is from Smalltalk, "Collecting Parameter":
当你需要收集多个方法的运行结果,比较好的一个方式就是每个方法增加一个参数,用这个参数object来收集结果
3.
protected void run(final TestCase test) {
startTest(test);
Protectable p= new Protectable() {
public void protect() throws Throwable {
test.runBare();
}
};
runProtected(test, p);
endTest(test);
}
提供可继承实现run方法,其中的protect()方法的具体实现也可以另外写,这里保留了runProtected()方法不用被继承。
4. No stupid subclasses
while (Test.class.isAssignableFrom(superClass)) {
Method[] methods= superClass.getDeclaredMethods(); //得到所有public的方法
for (int i= 0; i < methods.length; i++) {
addTestMethod(methods[i], names, theClass); //判断是否以test开头,返回类型是void,无参数,如果是,则加入names中
}
superClass= superClass.getSuperclass();
}
if (fTests.size() == 0)
addTest(warning("No tests found in "+theClass.getName()));
private void addTestMethod(Method m, Vector names, Class theClass) {
String name= m.getName();
if (names.contains(name))
return;
if (! isPublicTestMethod(m)) {
if (isTestMethod(m))
addTest(warning("Test method isn't public: "+m.getName()));
return;
}
names.addElement(name);
addTest(createTest(theClass, name));
}
static public Test createTest(Class theClass, String name) {
Constructor constructor;
try {
constructor= getTestConstructor(theClass);
} catch (NoSuchMethodException e) {
return warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()");
}
Object test;
try {
if (constructor.getParameterTypes().length == 0) {
test= constructor.newInstance(new Object[0]); //***********这里是关键,根据各个方法的名字创建了若干个TestCase对象,以方法的名字命名。
if (test instanceof TestCase)
((TestCase) test).setName(name);
} else {
test= constructor.newInstance(new Object[]{name});
}
} catch (InstantiationException e) {
return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
} catch (InvocationTargetException e) {
return(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
} catch (IllegalAccessException e) {
return(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
}
return (Test) test;
}
5. 不用去考虑一个或多个 -- TestSuite
TestSuite和TestCase都实现了Test接口,TestSuite中放了一个Vector,可以放TestSuite/TestCase这些实现了Test接口的对象。
6. 总结
7.其他
theClass.getConstructor(args); 得到一个类的构造函数,其仅含有一个String的参数
theClass.getConstructor(new Class[0]); 得到一个类的无参数构造函数
上面两个返回的是Constructor
Modifier.isPublic(theClass.getModifiers()) 可以判断一个类是否是public的
Modifier.isPublic(m.getModifiers() 可以判断一个方法是否是public的
Test.class.isAssignableFrom(superClass)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.
分享到:
相关推荐
Junit学习笔记,希望有用~~~~~~~~~~~~~~~~~~~~~~
Junit学习笔记和课件,欢迎学习爱好者下载资源,共同学习。。
JUnit是一款由Erich Gamma(《设计模式》的作者)和Kent Beck(极限编程的提出者)编写的开源的回归测试框架,供Java编码人员做单元测试之用。当前版本4.1,可以从www.junit.org网站上获得。与早期的JUnit 3相比,...
JUnit知识点滴集合笔记,这个包里面包括了好几篇文档,一定会让你对JUNIT有一个全新的认识。 JUnit 4 抢先看.doc JUnit in java 真正的测试用例实战.doc JUnit起步.doc junit实现过程.doc JUnit中如何测试异常.doc ...
NULL 博文链接:https://greatjone.iteye.com/blog/1161009
Junit4单元测试个人笔记Junit4单元测试个人笔记Junit4单元测试个人笔记
JUnit学习笔记之NetBeans入门篇,一句话好东西
现在的版本都4.3.1,3.8.1的感觉好象不好找,虽然www.junit.org能够找到,但对我们这种英语不好的来说,还是不好找,再说了,现在好多的教程都是以3.8.1来写的,所以在这里把我找到的3.8.1传上来供大家以后下载 博文链接:...
junit单元测试——浅浅笔记
Junit良葛格学习笔记,喜欢的下啊!!!
JUnit4是JUnit框架有史以来的最大改进,其主要目标便是利用Java5的Annotation特性简化测试用例的编写。 先简单解释一下什么是Annotation,这个单词一般是翻译成元数据。元数据是什么?元数据就是描述数据的数据。也...
junit4测试源码,看浪曦JUNIT4笔记
NULL 博文链接:https://rwl6813021.iteye.com/blog/511660
JUnit学习笔记及练习用例,主要分为JUnit3.8.2和JUnit4.8.1两个版本。
中间有各种Junit文档,Junit各种操作手册!3.8--4.9。还有本人学习的笔记。绝对值得下载。
以一个简单的DEMO介绍如何在Eclipse下使用JUnit4。供初次接触JUnit的同学学习
NULL 博文链接:https://thisliy.iteye.com/blog/786293