how to unit test file operations

View: New views
3 Messages — Rating Filter:   Alert me  

how to unit test file operations

by suckasholasdf :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a txt file that I need to update.  The txt file has the following columns, delimited by tabs:

Date
Name
Age


Then I have a method called updateFile().  The implementation is below:

public void updateFile() {
//open up file for writing, setting the append flag to 'true'
//write X lines to file
//close file
}

I'm not exactly sure how to write a test for this particular method.  I'm thinking I should check the size of the file.  But, that might not be enough since I also would probably have to check and validate the values being put into those columns.  

Now, after I'm done performing the test, the file will have X lines appended to it.  How do I revert back to it's original state if I have another test that depends on the txt file?

Please help!  Thanks!


RE: how to unit test file operations

by Forsberg, Mike-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you change your API a little bit it will make for easier testing...

 

Think about the second line of pseudo code as a method.

 

void writeToStream(OutputStream out) {

   // perform write

}

 

Now you can test the writing of data using a mock object.  Avoiding the
messy opening of the file to check contents.  Which is what you are
trying to avoid.

 

For opening the file and setting the append flag, this can be checked by
reflection.  I'd make a method for that bit of code as well.

 

Testing the entire composition could be done be visual inspection.
(Your sure the parts work, so the wiring is all that is needed to be
inspected.)

 

That's my ten minutes quick instinct idea.  Sorry it's not complete...

 

Big Mike

 

From: junit@... [mailto:junit@...] On Behalf Of
suckasholasdf
Sent: Thursday, October 22, 2009 4:05 AM
To: junit@...
Subject: [junit] how to unit test file operations

 

 

I have a txt file that I need to update. The txt file has the following
columns, delimited by tabs:

Date
Name
Age

Then I have a method called updateFile(). The implementation is below:

public void updateFile() {
//open up file for writing, setting the append flag to 'true'
//write X lines to file
//close file
}

I'm not exactly sure how to write a test for this particular method. I'm
thinking I should check the size of the file. But, that might not be
enough since I also would probably have to check and validate the values
being put into those columns.

Now, after I'm done performing the test, the file will have X lines
appended to it. How do I revert back to it's original state if I have
another test that depends on the txt file?

Please help! Thanks!





[Non-text portions of this message have been removed]


RE: how to unit test file operations

by kentb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The first thing I would do in this case is split the file opening/closing
from the content writing. Then I could test the two aspects separately. The
method to write content would take a stream as a parameter, so I could
easily gen up a stream in my test, call the method, and check the results.
 
Regards,
 
Kent

  _____  

From: junit@... [mailto:junit@...] On Behalf Of
Forsberg, Mike
Sent: Thursday, October 22, 2009 11:22 AM
To: junit@...
Subject: RE: [junit] how to unit test file operations


 

If you change your API a little bit it will make for easier testing...

Think about the second line of pseudo code as a method.

void writeToStream(OutputStream out) {

// perform write

}

Now you can test the writing of data using a mock object. Avoiding the
messy opening of the file to check contents. Which is what you are
trying to avoid.

For opening the file and setting the append flag, this can be checked by
reflection. I'd make a method for that bit of code as well.

Testing the entire composition could be done be visual inspection.
(Your sure the parts work, so the wiring is all that is needed to be
inspected.)

That's my ten minutes quick instinct idea. Sorry it's not complete...

Big Mike

From: junit@yahoogroups. <mailto:junit%40yahoogroups.com> com
[mailto:junit@yahoogroups. <mailto:junit%40yahoogroups.com> com] On Behalf
Of
suckasholasdf
Sent: Thursday, October 22, 2009 4:05 AM
To: junit@yahoogroups. <mailto:junit%40yahoogroups.com> com
Subject: [junit] how to unit test file operations

I have a txt file that I need to update. The txt file has the following
columns, delimited by tabs:

Date
Name
Age

Then I have a method called updateFile(). The implementation is below:

public void updateFile() {
//open up file for writing, setting the append flag to 'true'
//write X lines to file
//close file
}

I'm not exactly sure how to write a test for this particular method. I'm
thinking I should check the size of the file. But, that might not be
enough since I also would probably have to check and validate the values
being put into those columns.

Now, after I'm done performing the test, the file will have X lines
appended to it. How do I revert back to it's original state if I have
another test that depends on the txt file?

Please help! Thanks!

[Non-text portions of this message have been removed]






[Non-text portions of this message have been removed]