如何用Junit测试读取properties文件的类?         谢谢~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~这个properties文件可能有20种情况
我现在的想法是在junit测试代码里去生成这20种情况的文件.
例:
testXXXX1() {
    生成第一种情况的properties文件
     执行被测试程序
     assert判断
}testXXXX2() {
    生成第二种情况的properties文件
     执行被测试程序
     assert判断
}・第一次用junit不知道这样想对不对?
・如果这样可以的话,如何保证生成properties文件的代码是正确的呢? 谁来测试生成properties代码呢?

解决方案 »

  1.   

    这就跟用 Java 来写代码,如果 Java 类库本身存在 bug,那么你写出来的
    代码就有可能有 bug。用 JUnit 写单元测试代码也是一样,我感觉是没人能保证所写的 JUnit 是百
    分之一百正确的,或者这样说,通过 JUnit 测试的并不能保证代码是完全正确
    的,除了测试不全这种因素之外,还有可能就是测试代码写错了。当然了,如果
    程序是正确的,测试代码写错了,测试也会失败。呵呵,没做过测试,不知道测试人员对这种问题是如何解决的。
      

  2.   

    测试用的文件应该事先准备好。当然测试的预期结果也应该准备好。
    OVER
      

  3.   


    1.testAll(){
       for iterators...
          生成properties文件
          执行被测试程序 
          assert判断 
    }
    2.如果这样可以的话,如何保证生成properties文件的代码是正确的呢? 谁来测试生成properties代码呢?--->  The method to generate properties files needs to be tested as well.
      

  4.   

    生成properties文件的代码也是你的被测对象的功能吗,如果不是就应假设它是正确的,测试总是优先考虑学要测试的对象。其他方面可以另外设计测试用例,或者交有其他类型的设测。因为,不管是什么测试方案总不可能面面具到,好的测试方案应该详细说明了需要测试什么,不需要测试什么。如果是你需要测试的部分,那你再单独准备一个test方法,放在所有其他test方法的前面,这样首先测试的就是那个代码了。我建议,你生成properties文件的代码放在setUp方法里,变且所有可能以数组的形式保存。比如我的一个例子:
    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文件是事先准备好的以上源于自己的操作。