2013-03-05 99 views
1
public void createRootElement() throws FileNotFoundException, IOException 
    { 
    Properties prop = new Properties(); 
    prop.load(new FileInputStream("/home/asdf/Desktop/test.properties")); 
     File file = new File(prop.getProperty("filefromroot")); 
     try 
      { 
       // if file doesn't exists, then create it 
       if (!file.exists()) 
        { 
         file.createNewFile(); 
        } 
       FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
       BufferedWriter bw = new BufferedWriter(fw); 
       bw.write("<root>"); //create the root tag for the XML File. 
       bw.close(); 
      } 
     catch(Exception e) 
      { 
      writeLog(e.getMessage(),false); 
      } 
    } 

我是junit testing的新手。我想知道如何编写测试用例,以及需要考虑的全部内容。如何调用该方法从此测试中调用。从一次测试中调用某个方法时的junit测试

+0

这应该让你开始:http://junit.sourceforge.net/doc/faq/faq.htm – Aboutblank 2013-03-05 18:31:53

回答

2

JUnit测试用例应该是这样的:

import static org.junit.Assert.assertTrue; 
import org.junit.Test; 

public class ClassToBeTestedTest { 

    @Test 
    public void test() { 
     ClassToBeTested c = new ClassToBeTested(); 
     c.createRootElement(); 
     assertTrue(c.rootElementExists()); 
    } 

} 

您标记与@Test标注的测试方法,并编写执行你要测试的代码。

在这个例子中,我创建了一个类的实例并调用createRootElement方法。

之后,我做了一个断言来验证一切是否像我预期的那样。

有许多事情你可以断言。阅读JUnit文档以获取更多信息。

一个好的做法是在您实际编写代码之前编写测试。因此,测试将指导您如何编写更好的代码。这被称为TDD。谷歌为它。