| Working with temporary files in JUnit 4.7 |
|
|
Another handy feature in JUnit 4.7 is the TemporaryFolder @Rule. Using this rule, JUnit will create a temporary folder before your test, and delete it afterwards, whether the test passes or fails. This comes in very handy for tests involving file manipulation of any sort. Of course you can write this code yourself (and most of us have!), but any infrastructure coding in tests is tiresome at best, and at worst will discourage developers (present company excluded, of course ;-)) from doing the sort of thorough testing that any file-based functionality really requires. So any build-in help in this area is very welcome.The TemporaryFolder @Rule is easy to use: you just create a TemporaryFolder variable using the @Rule annotation as shown below, and then use this variable to create your files and directories in your tests. In the example shown below, we are testing the DynamicMessageBundle class, an imaginary class that extends a standard Properties file with features such as being able to be reloaded dynamically or from different data sources. In the test shown here, we are testing that we can load the file from a standard properties file. In a @Before method, we create a properties file in the temporary folder created by JUnit. We then use this file for our tests. It will be destroyed, along with the temporary folder, after the test has finished.
The TemporaryFolder is not a File object as such: you use methods such as
Bookmark
Email this
Trackback(0)
Comments (2)
![]() written by ebagsell, April 13, 2010
louis vuittonEU joint aid mechanism baked 30 billion euros aid Greecelouis vuittonFriends
Write comment
|
|