I have a lot of JUnit tests in my projects. Some of them have some side-effects that can influence the execution of other tests. For example, they open database connections and forget to close them, and then at some point tests start to fail because they reached the limit. I thought that I could use some mixins for the test, that I could use like this...
@CheckThatConnectionsAreClosed
@LogExecutionTime
@Test
public void testSomething() {
// ...
sessionFactory.openSession(); // hey look! I didn't close it!
// ...
}
...so that when I run it, I'd get an assertion error plus information about the execution time.
I mean, checking that connections are closed is simple, but putting code manually to @Before
and @After
method in each test class or method seems like a bad idea.
Those annotations I think of would be effectively method interceptors and I guess that it would be possible with a custom test runner. But maybe there is some simpler way to achieve this? Or maybe it's already done in some good library?