8

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.

catch23
  • 17,519
  • 42
  • 144
  • 217

3 Answers3

12

You probably don't need mocks at all. Try using StringWriter in your tests.

// 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()); 
}
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • 4
    [@VisibleForTesting](https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/annotations/VisibleForTesting.java) is a handy documentation annotation in [Guava](https://code.google.com/p/guava-libraries/). It isn't necessary for the code at all, it's just a warning to your users not to call that method outside of tests. – Jeff Bowman Jan 18 '13 at 17:07
8

I'll suggest you making an interface wrapper around you IO classes (the PrintWriter class in your case) so you can use mock objects for output. You don't have to test Java PrintWriter, you want to test your functionality, right?

So your class will be

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!");
        }
    }
}

The signature of the MyWriter interface is pretty straightforward.

interface MyWriter {

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

    void close();

}

Then you can use EasyMock to write a test. The test method will be something like

@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);
}
Vic
  • 1,778
  • 3
  • 19
  • 37
  • 1
    You can test reading in a similar fashion. In my example no interaction with the real IO is made. It depends what exactly you want to test: your storage logic or that the interaction with the OS went well and the data are stored correctly? Easymock just tests that the specific methods are called with specific parameters etc. – Vic Jan 13 '13 at 13:51
  • 3
    Another idea would be creating you own mock implementation that will remember the contents you write in a (probably static) map `Map> fileNameToContentsMap`. In this case you wuld be able to emulate reading and writing as if it works with a real file system. – Vic Jan 13 '13 at 13:55
  • 1
    Check this one out, it might be helpful. I remember I answered a similar question. http://stackoverflow.com/questions/11849728/simulate-file-in-java/11849881#11849881 – Vic Jan 13 '13 at 14:01
  • Can you recomend some tutorials about Mocking? – catch23 Mar 23 '13 at 05:09
  • I don't know any. I learned it from the documentation of EasyMock: http://www.easymock.org/Documentation.html. – Vic Mar 25 '13 at 10:46
0

You seem to have the right idea so I am not sure what you need help with exactly...

can we join both tests(write/read)

yes.

or better do this individually(coz if our test fall we don't know where is problem - in read or write)?

Better but harder to maintain.

How should make this correctly at junit(with prepare to test, and test itself)?

Create some dummy data, in code or in a text file.

In the first case, write out the file and read it back in as check it is the same.

In the second case, you can read the text file and write it out again, and check it is the same.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130