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测试的新手。我想知道如何为此编写测试用例以及需要考虑的内容。如何调用该方法是从这个测试中调用的。?
答案 0 :(得分: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。谷歌吧。