How to check write to file in Java?

I start and keep myself in control. I have a simple program, and I need to run a junit test for the write method. I have a collection in the input. How can i do this? This is 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, because after that I read from the file whether we can join both tests (write / read) or is it better to do it individually (for example, if our test crashes, we don’t know where the problem is - when reading or writing )? How to do it correctly in junit (with preparation for testing and testing)? It is better to show an example to better understand.

Thanks, Nazar.

+4
source share
3 answers

I suggest you create an interface wrapper around you with IO classes (the PrintWriter class in your case) so that you can use mock objects for output. You do not need 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 MyWriter quite simple.

 interface MyWriter { void println(Object x); // You can add other println methods here. void close(); } 

Then you can use EasyMock to record the test. The testing method will look 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); } 
+6
source

You probably don't need layouts 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()); } 
+9
source

You seem to have the right idea, so I'm not sure you need help for sure ...

can we join both tests (write / read)

Yes.

or is it better to do it individually (if we lose the test, we don’t know where the problem is in reading or writing)?

Better, but harder to maintain.

How to do it correctly in junit (with preparation for testing and testing)?

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

In the first case, write out the file and read it back, since check that it is one and the same.

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

0
source

All Articles