如何用Junit测试读取properties文件的类? 谢谢~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~这个properties文件可能有20种情况
我现在的想法是在junit测试代码里去生成这20种情况的文件.
例:
testXXXX1() {
生成第一种情况的properties文件
执行被测试程序
assert判断
}testXXXX2() {
生成第二种情况的properties文件
执行被测试程序
assert判断
}・第一次用junit不知道这样想对不对?
・如果这样可以的话,如何保证生成properties文件的代码是正确的呢? 谁来测试生成properties代码呢?
我现在的想法是在junit测试代码里去生成这20种情况的文件.
例:
testXXXX1() {
生成第一种情况的properties文件
执行被测试程序
assert判断
}testXXXX2() {
生成第二种情况的properties文件
执行被测试程序
assert判断
}・第一次用junit不知道这样想对不对?
・如果这样可以的话,如何保证生成properties文件的代码是正确的呢? 谁来测试生成properties代码呢?
代码就有可能有 bug。用 JUnit 写单元测试代码也是一样,我感觉是没人能保证所写的 JUnit 是百
分之一百正确的,或者这样说,通过 JUnit 测试的并不能保证代码是完全正确
的,除了测试不全这种因素之外,还有可能就是测试代码写错了。当然了,如果
程序是正确的,测试代码写错了,测试也会失败。呵呵,没做过测试,不知道测试人员对这种问题是如何解决的。
OVER
1.testAll(){
for iterators...
生成properties文件
执行被测试程序
assert判断
}
2.如果这样可以的话,如何保证生成properties文件的代码是正确的呢? 谁来测试生成properties代码呢?---> The method to generate properties files needs to be tested as well.
package net.wargreytest.io;import net.wargrey.application.ExceptionManager;
import net.wargrey.io.DirectoryNotFoundException;
import java.io.File;import junit.framework.TestCase;import net.wargrey.io.Directory;public class DirectoryTest extends TestCase{
private Directory [] testCases=new Directory[4];
private Directory root=null;
private Directory home=null;
private Directory cur=null;
private Directory notexist=null; @Override
public void setUp(){
try{
this.testCases[0]=this.root=new Directory("/");
this.testCases[1]=this.home=new Directory("/home/Estelle");
this.testCases[2]=this.cur=new Directory(".");
this.testCases[3]=this.notexist=new Directory("/home/Estelle/Yoshua");
}catch(Exception npe){
fail("Initail failure!");
}
} public void testListRoots(){//测试静态方法
Directory [] roots=Directory.listRoots();
assertEquals("The number of roots is 1 on linux",roots.length,1);
}
public void testConstructorExceptions(){//测试构造异常
Directory vim=null;
try{
vim=new Directory("/home/Estelle/Programs/bin/vim");
}catch(NullPointerException npe){
assertNull("Is there should be null?",vim);
}catch(DirectoryNotFoundException dnfe){
assertNull("Directory not found!",vim);
}
} public void testGetParentDirectory(){//测试普通方法示例
String [] results={null,"/home","/home/Estelle/Frameworks","/home/Estelle"};
String [] alerts={"Parent of root should be null","","",""};
for (int i=0;i<testCases.length;i++){
Directory parent=testCases[i].getParentDirectory();
if (results[i]==null){
assertNull(alerts[i],parent);
}else{
assertNotNull(alerts[i],parent);
assertEquals(alerts[i],parent.toString(),results[i]);
}
}
}
}
实际上,你生成properties文件的方法不应该在junit中给出,properties文件是事先准备好的以上源于自己的操作。