See which of your colleagues or former colleagues are already on Java Link: Check out the Contact Finder
News »Browse Articles » Working with temporary files in JUnit 4.7
0
Vote Vote

Working with temporary files in JUnit 4.7

Views 0 Views    Comments 0 Comments    Share Share    Posted 01-10-2009  
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.
view source
print?
01.public class DynamicMessageBundleTest {
02.
03. @Rule
04. public TemporaryFolder folder = new TemporaryFolder();
05.
06. private File properties;
07.
08. @Before
09. public void createTestData() throws IOException {
10. properties = folder.newFile("messages.properties");
11. BufferedWriter out = new BufferedWriter(new FileWriter(properties));
12. out.write("first.name = Arthur\n");
13. out.write("last.name = Dent\n");
14. out.write("favorite.object = Towel\n");
15. out.close();
16. }
17.
18. @Test
19. public void shouldLoadFromPropertiesFile() throws IOException {
20. assertThat(properties.exists(), is(true));
21.
22. DynamicMessageBundle bundle = new DynamicMessageBundle();
23. bundle.load(properties);
24. assertThat(bundle.getProperty("first.name"), is("Arthur"));
25. assertThat(bundle.getProperty("last.name"), is("Dent"));
26. assertThat(bundle.getProperty("favorite.object"), is("Towel"));
27. }
28.
29. @After
30. public void cleanUp() {
31. assertThat(properties.exists(), is(true));
32. }
33.}

The TemporaryFolder is not a File object as such: you use methods such as newFile() and newFolder() to create temporary files and directories. It does however conceptually represent a real temporary directory that JUnit has created for you - you can get hold of this temporary directory using the getRoot() method.

Source:
http://java.dzone.com/blogs/mrjohnsmart/2009/09/29/working-temporary-files-junit
0
Vote  Vote
Enter your comment:
No Comments For This News

Search News

What's the News?

Post a link to something interesting from another site, or submit your own original writing for the Java community to read.

Most Popular News

Most Recent User Submitted News