5

I'm beginner, and keep yourself in hands. I have some easy program, and I need do junit test for write method. I have some collection in input. How I can do this? This my code:

// write to file
public void write(String fileName, 
        List<FigureGeneral> figuresList) {
    try {
        PrintWriter out = new PrintWriter(
                new File(fileName).getAbsoluteFile());
        try {
            for (int i = 0; i < figuresList.size(); i++) {
                out.println(figuresList.get(i).toString());
            }
        } finally {
            out.close();
        }
    } catch (IOException e) {
        System.out.println("Cannot write to file!");
    }
}

And I want to know, coz after this I read from file, can we join both tests(write/read) or better do this individually(coz if our test fall we don't know where is problem - in read or write)? How should make this correctly at junit(with prepare to test, and test itself)? Better show on example, this way better to understand.

Thanks, Nazar.

4

3 回答 3

11

你可能根本不需要模拟。尝试在测试中使用 StringWriter。

// write to file
public void write(String fileName, List<FigureGeneral> figuresList) {
  try {
    Writer out = new FileWriter(new File(fileName).getAbsoluteFile());
    write(out, figuresList);
  } catch (IOException e) {
    System.out.println("Cannot write to file!");
  }
}

@VisibleForTesting void write(Writer writer, List<FigureGeneral> figuresList) {
  PrintWriter out = new PrintWriter(writer);
  try {
    for (int i = 0; i < figuresList.size(); i++) {
      out.println(figuresList.get(i).toString());
    }
  } finally {
    out.close();
  }
}

@Test public void testWrite() {
  List<FigureGeneral> list = Lists.newArrayList();
  list.add(...); // A
  list.add(...); // B
  list.add(...); // C
  StringWriter stringWriter = new StringWriter();
  write(stringWriter, list);
  assertEquals("A.\nB.\nC.\n", stringWriter.toString()); 
}
于 2013-01-17T02:12:42.560 回答
8

我建议您围绕 IO 类(PrintWriter您的案例中的类)制作一个接口包装器,以便您可以使用模拟对象进行输出。您不必测试 Java PrintWriter,您想测试您的功能,对吗?

所以你的课将是

class MyClass {

    MyWriter out;

    public void setOut(MyWriter out) {
        this.out = out;
    }

    // write to file
    public void write(String fileName, List<FigureGeneral> figuresList) {
        try {
            try {
                for (int i = 0; i < figuresList.size(); i++) {
                    out.println(figuresList.get(i).toString());
                }
            } finally {
                out.close();
            }
        } catch (IOException e) {
            System.out.println("Cannot write to file!");
        }
    }
}

界面的签名MyWriter非常简单。

interface MyWriter {

    void println(Object x); // You can add other println methods here.

    void close();

}

然后你可以使用EasyMock来写一个测试。测试方法将类似于

@Test
public void testWrite() {
    MyWriter out = EasyMock.createMock(MyWriter.class);
    EasyMock.expect(mock.println(EasyMock.anyObject())).times(3);
    EasyMock.expect(mock.close()).times(1);

    List<FigureGeneral> list = ...
    list.add(...);
    list.add(...);
    list.add(...);

    replay(mock);

    MyClass myClass = new MyClass();
    myClass.setOut(out);
    myClass.write("mockFileName", list);        

    verify(mock);
}
于 2013-01-12T15:52:17.107 回答
0

你似乎有正确的想法,所以我不确定你到底需要什么帮助......

我们可以加入两个测试吗(写/读)

是的。

或者更好地单独执行此操作(因为如果我们的测试失败,我们不知道问题出在哪里 - 在读取或写入中)?

更好但更难维护。

应该如何在junit正确地做到这一点(准备测试,并测试自己)?

在代码或文本文件中创建一些虚拟数据。

在第一种情况下,写出文件并将其读回,以检查它是否相同。

在第二种情况下,您可以读取文本文件并再次将其写出来,并检查它是否相同。

于 2013-01-12T15:37:17.393 回答