views:

96

answers:

4

I know this question is not a hard one.

I want to implements jUnit on a small project I'm working on because I want to learn a little bit about it.

The tutorials that I read all make reference to method that have a particular output.

In my case my output are files, how can I do this? any simple example? any aproach that could help me with this?

the files are raw text files, that are build by a void private method.

Thanks for any helps with this.

+1  A: 

After your methods write the file, in the unit-test you can read the file and verify whether it is written correctly.

Another thing that makes sense is to have your methods split in one that retrieves that data and returns it to the methods that merely writes it to a file. Then you can verify whether the data returned by the first method is fine.

And another plausible approach would be to pass an OutputStream to the method that writes the data. In the "real code" you can pass a FileOutputStream / FileWriter, while in the test-code you can write a mock implementation of OutputStream and check what is being written to it.

Bozho
+2  A: 

If you can't control the method to put the output in a stream, then I'd say you need to refactor your code so that the method recieves a stream in the parameter (or in the c'tor of its class).

After that, testing is pretty easy - you can just check the stream. Easily testable code usually equals good code.

Amir Rachum
The problem is that actually it doesn't make one single file, it makes between 3 and 5 files depending on different things. But because I'm really noob with junit I wanted to try it first with one single file to understand it completely. Thanks =D.
Saikios
@Saikios this is relevant for any number of files :)
Amir Rachum
+1  A: 

You want to get a correct output file for a given set of inputs, and setup a test to call your void method with those inputs, and then compare your validated output file against whats produced by your method. You need to make sure that you have some way of specifying where your method will output to, otherwise your test will be very brittle.

private static final File dir = new File("tmp");

@Before
public void setUp() {
    Assert.assertTrue("Unable to create " + dir.getAbsoultePath(), dir.exists() || dir.mkDirs());
}

@Test
public void testXYZ() {
    final File expected = new File("xyz.txt");
    final File output = new File(dir, "xyz.txt");
    TestClass.xyz(output);
    Assert.assertEquals(FileUtils.readLines(expected), FileUtils.readLines(output));
}

Uses commons-io FileUtils for convinience text file comparison.

Jon Freedman
I like this, but why nobody vote for your answer? :S
Saikios
You can always vote for it yourself
Jon Freedman
eclipse says that org.junit.internal.runners.TestClass is deprecated =(
Saikios
Jon Freedman
A: 

Although your question may seem simplistic it does strike to the heart of unit testing, one needs to write well formed code that is testable. This is why some experts advise that one should write the unit test first and then the implementing class.

In your case I suggest you allow your method to execute and create the file(s) expected, following which your unit test(s) can analyse that the files are formed correctly.

Ashley Walton
Thanks, I will have it in mind for next time to first make my unit test :D
Saikios