You may also look at the transient repository provided by the Jackrabbit implementation.
(assuming you are using Jackrabbit implementation)
A Transient Repository is
a repository proxy that automatically initializes and shuts down the underlying repository instance when the first session is opened or the last one closed.
- API doc
Please look at the examples here - it is really easy to create an instance of it. Since it is a local repository, you will never need to go over the network.
I normally create a transient repository and a session once per JUnit test class in setUpClass/BeforeClass hook and reuse it across all the test cases, cleaning up the session ( e.g, removing the nodes I added during a test case, etc.) after each test case.
One drawback, however, using transient repository, is that it will create several repository specific files/directory in your base directory. If you are using maven, then you can use maven-clean-plugin to clean up the unwanted files as a solution.