Reputation: 41509
Today I bumped into my own mess: some gigabytes of temporary test data in temp folders created by JUnit's TemporaryFolder
s that weren't cleaned up.
It appears to be that if your test does not have a @After
method, the @Rule
s are not after()
ed, either.
Is there a way to assure (programmatorically) that a test with a @Before
method also has at least one @After
method, too?
Upvotes: 1
Views: 109
Reputation: 123890
Presence of absence of an @After
method has no effect on JUnit rules. In general, @After
is used much less often than @Before
in JUnit tests. If the TemporaryFolder
rule didn't clean up, then it probably wasn't successful deleting the files, for example due to a file permission problem. As you can tell from TemporaryFolder
's source code, you won't get an error or warning in such a case (the return value of File.delete
isn't used). Another possibility is that the JVM crashed or was terminated by the party controlling test execution (IDE, build tool, CI server).
Upvotes: 4
Reputation: 2107
You could run through all your test files and examine the contents to make sure that is @Before is present then @After is as well. I do this kind of thing quite a bit, and I find it runs sufficiently swiftly to include in a unit test suite.
Of course this won't prove that you are doing the right thing in your @After method.
Something like this should get you started
for (File file : new File(yourTestDirectory).listFiles()) {
for (String contents : Files.toString(file, Charset.defaultCharset())) {
if(contents.contains("@Before")){
if(!contents.contains("@After"){
fail("must have @Before and @After");
}
}
}
}
Upvotes: 0