java - 如何测试从文件和路径读取(使用 junit)?

标签 java testing file-io junit path

我需要使用 JUnit 框架执行测试覆盖率。

我读到JUnit - Tutorial但我无法将这些知识与我的例子联系起来。

我了解如何测试单独从文件读取的方法,但不知道如何通过测试某些路径和askUserPathAndWord 来完全做到这一点。我怎样才能对此进行良好的测试?

package task;
import java.io.*;

class SearchPhrase {
    public void walk(String path, String whatFind) throws IOException {
        File root = new File(path);
        File[] list = root.listFiles();
        for (File titleName : list) {
            if (titleName.isDirectory()) {
                walk(titleName.getAbsolutePath(), whatFind);
            } else {
                if (read(titleName.getAbsolutePath()).contains(whatFind)) {
                    System.out.println("File:" + titleName.getAbsolutePath());
                }
            }
        }
    }

    // Read file as one line
    public static String read(String fileName) {
        StringBuilder strBuider = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new FileReader(new File(
                    fileName)));
            String strInput;
            while ((strInput = in.readLine()) != null) {
                strBuider.append(strInput);
                strBuider.append("\n");
            }

            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return strBuider.toString();
    }

    public void askUserPathAndWord() {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(System.in));
        String path, whatFind;
        try {
            System.out.println("Please, enter a Path and Word"
                    + "(which you want to find):");
            System.out.println("Please enter a Path:");
            path = bufferedReader.readLine();
            System.out.println("Please enter a Word:");
            whatFind = bufferedReader.readLine();

            if (path != null && whatFind != null) {
                walk(path, whatFind);
                System.out.println("Thank you!");
            } else {
                System.out.println("You did not enter anything");
            }
        } catch (IOException | RuntimeException e) {
            System.out.println("Wrong input!");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
            SearchPhrase example = new SearchPhrase();
            example.askUserPathAndWord();
    }
}

接下来的问题:

  • 我们如何完整测试集成依赖项并检查路径?
  • 哪一点应该有良好的、易于理解的 junit 测试?
  • 在这种情况下我们需要使用失败测试吗?
  • 我们可以覆盖的最大百分比计划是多少?
  • 我们是否应该测试私有(private)方法和 protected 方法(一般情况下)?

最佳答案

我建议一开始就保持简单。在我看来,从根本上讲,编写单元测试的目标实际上只是确保您的代码能够完成其预期的任务。

我认为您的 read 方法是一个很好的起点。看起来 read 方法的目的是将文件名作为输入并返回包含文件内容的字符串。

因此,要编写一个测试来证明其有效,请首先在某个位置创建一个名为“testFile”的文件,并在文件中包含一些虚拟文本,例如“my test”。

然后,编写一个单元测试,例如:

@Test
public void read() {

    String testFileName = "/path/to/test/file/testFile";
    String expected = "my test";
    SearchPhrase searchPhrase = new SearchPhrase();
    String result = searchPhrase.read(testFileName);
    assertEquals(expected, result);

}

这只是为了让您开始。一旦掌握了窍门,您就可以进行改进,例如更新测试文件的路径,使其不再被硬编码。理想情况下,测试应该能够从任何地方运行。

作为一个额外的好处,编写单元测试可以让您考虑以一种更可重用且更易于理解的方式组织您的包、类和方法(除了其他好处之外)。

例如,您的 walk 方法很难测试,因为它是当前编写的。因此,尝试并思考如何使其更容易测试。也许它可以返回代表搜索结果的字符串列表?如果您进行更改并在包含 testFile 的目录中搜索字符串“test”,您知道您应该在列表中得到一个结果,因此请使用以下内容进行测试:

assertNotNull(searchPhrase.walk());
assertEquals(1, searchPhrase.walk().size());

由于您刚刚开始,我建议不要担心测试覆盖的程序百分比。

在编写测试时,它可以帮助我思考如果其他人使用我的代码,他们会期望它如何表现?然后我尝试编写测试来演示预期的行为。

希望有帮助!

关于java - 如何测试从文件和路径读取(使用 junit)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14527074/

相关文章:

java - 错误填充异常 : Given final block not properly padded

java - 开始时的 LeanFT 空指针

python - 如何使用 numpy 保存和读回多维字符串数组(可能)?

android - 安卓应用卸载后保留文件

c++ - 如何从输入文件 C++ 的行中读取多个数据变量?

使用 Java SWING 的 Java 2d 游戏

java - AWS Redshift Posix 模式匹配

testing - 使用具有随机值的 Selenium IDE

java - Mockito 模拟方法返回 NULL

java - Scribe-LinkedIn 搜索 API