Java:如何测试save方法?

Java:如何测试save方法?,java,Java,我有以下保存方法,但我不知道如何验证该方法是否正常工作。如何在测试类中验证它 static void saveFile(List<String> contents, String path){ File file = new File(path); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); for(String data : contents){

我有以下保存方法,但我不知道如何验证该方法是否正常工作。如何在测试类中验证它

 static void saveFile(List<String> contents, String path){

   File file = new File(path);
   PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));

   for(String data : contents){
      pw.println(data);
   }
 }
静态void保存文件(列表内容、字符串路径){
文件=新文件(路径);
PrintWriter pw=新的PrintWriter(新的缓冲写入程序(新的文件写入程序(文件)));
for(字符串数据:内容){
pw.println(数据);
}
}

对不起,内容不是字符串,而是列表。但是有没有必要做测试课??因为它是由经过测试的java方法构造的。

像这样从您的方法中删除
FileWriter

static void saveFile(List<String> contents, Writer writer){
   PrintWriter pw = new PrintWriter(new BufferedWriter(writer));

   for(String data : contents){
      pw.println(data);
   }

   pw.flush();
}
在你的真实代码中

...
Writer writer = new FileWriter(new File(path));
saveFile(Arrays.asList("real content", "real content2"), writer);
...

像这样从您的方法中删除
FileWriter

static void saveFile(List<String> contents, Writer writer){
   PrintWriter pw = new PrintWriter(new BufferedWriter(writer));

   for(String data : contents){
      pw.println(data);
   }

   pw.flush();
}
在你的真实代码中

...
Writer writer = new FileWriter(new File(path));
saveFile(Arrays.asList("real content", "real content2"), writer);
...

测试时,您可以考虑一个测试框架,例如JUnit,并编写测试用例。在您的具体案例中,您可以编写以下内容:

public class TestCase {

    @Test
    public void test() throws IOException {
        String contents = "the your content";
        String path = "the your path";

        // call teh metod
        saveFile(contents, path);

        // tacke a reference to the file
        File file = new File(path);

        // I assert that the file is not empty
        Assert.assertTrue(file.length() > 0);

        // I assert that the file content is the same of the contents variable
        Assert.assertSame(Files.readLines(file, Charset.defaultCharset()).stream().reduce("", (s , s2) -> s+s2),contents);
    }


    static void saveFile(String contents, String path) throws IOException {

        File file = new File(path);
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));

        pw.println(contents);
    }
}

这样,您就有了一个框架来检查您的代码是否按预期工作。如果这还不够,您应该查看一个模拟框架,如Mokito。

<测试>,您可以考虑一个测试框架,例如JUnit,并编写测试用例。在您的具体案例中,您可以编写以下内容:

public class TestCase {

    @Test
    public void test() throws IOException {
        String contents = "the your content";
        String path = "the your path";

        // call teh metod
        saveFile(contents, path);

        // tacke a reference to the file
        File file = new File(path);

        // I assert that the file is not empty
        Assert.assertTrue(file.length() > 0);

        // I assert that the file content is the same of the contents variable
        Assert.assertSame(Files.readLines(file, Charset.defaultCharset()).stream().reduce("", (s , s2) -> s+s2),contents);
    }


    static void saveFile(String contents, String path) throws IOException {

        File file = new File(path);
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));

        pw.println(contents);
    }
}

这样,您就有了一个框架来检查您的代码是否按预期工作。如果这还不够,您应该研究Mockito之类的模拟框架。

创建另一个名为loadFile的方法,读取写入的数据并验证两种情况下的内容是否相同为什么要测试Java标准类?您的方法中没有需要测试的逻辑。您没有关闭方法中的PrintWriter,因此它不会将所有行完全写入文件。另外,您确实意识到已经有了一个标准的方法
文件。write
可以做同样的事情,不是吗?您想测试文件在运行时是否正确写入吗?或者您想在编写应用程序时检测自己的故障?创建另一个名为loadFile的方法,读取写入的数据,并验证两种情况下的内容是否相同为什么要测试Java标准类?您的方法中没有需要测试的逻辑。您没有关闭方法中的PrintWriter,因此它不会将所有行完全写入文件。另外,您确实意识到已经有了一个标准的方法
文件。write
可以做同样的事情,不是吗?您想测试文件在运行时是否正确写入吗?或者您想在编写应用程序时检测自己的失败吗?writer.toString()不会返回已写入的错误content@Pooya会的。阅读carefully@Pooya您还应该使用
writer.flush()
,以确保您的内容真正写入了writer。测试和实际使用都是如此。writer.toString()不会返回写入的content@Pooya会的。阅读carefully@Pooya您还应该使用
writer.flush()
,以确保您的内容真正写入了writer。测试和实际使用都是如此。你不需要测试真正的文件编写,只需要测试编写的逻辑。我同意你的看法,安德烈,我的答案是作为技术支持,换句话说,如果你想在问题中提到的方法上执行测试用例,那么选择一个测试框架,并在我的answare中展示一种使用jUnit api的方法。就这样。你不需要测试真正的文件编写,只需要测试编写的逻辑。我同意你的看法,安德烈,我的答案是作为技术支持,换句话说,如果你想在问题中提到的方法上执行测试用例,那么选择一个测试框架,并在我的answare中展示使用JUnitAPI的方法。就这样。