Managing database connections for unit tests I have a series of unit test classes that use various connections to a database. Every test class uses two methods with the JUnit annotations BeforeClass and AfterClass to start and stop the database connections like so:

public class AccountTest
{
    private static DatabaseConnection _adminConnection;
    private static DatabaseConnection _salesUserConnection;
    private static DatabaseConnection _salesManagerConnection;
    private static DatabaseConnection _financeUserConnection;


    private static DatabaseHelper _helper;

    private static ArrayList&lt;DatabaseConnection&gt; _connections = new ArrayList&lt;&gt;();

    @BeforeClass
    public static void CreateTestEnvironment()
    {
        _helper = new DatabaseHelper();

        _adminConnection = _helper.DoLogin(SessionType.Administrator);
        _salesUserConnection = _helper.DoLogin(SessionType.SalesUser);
        _salesManagerConnection = _helper.DoLogin(SessionType.SalesManager);
        _financeUserConnection = _helper.DoLogin(SessionType.FinanceUser);

        _connections.add(_adminConnection);
        _connections.add(_salesUserConnection);
        _connections.add(_salesManagerConnection);
        _connections.add(_financeUserConnection);
    }

    @AfterClass
    public static void DestroyTestEnvironment()
    {
        for(DatabaseConnection connection: _connections)
        {
            _helper.DoLogout(connection);
        }
    }
}


I would then pass the connection to my helper object in a test like this for example:

@Test
public void AdministratorCanCreateAccount()
{
    Account account = new Account();
    account = _helper.CreateObject(account, _adminConnection);

    Assert.assertNotNull(account.getId());
}


The login/logout process in CreateTesEnvironment and DestroyTestEnvironment is duplicated throughout many different test classes, with some classes having more connections than others. 

How could I manage the connections in a better way so that I'm not just copying and pasting the same BeforeClass and AfterClass code every time but keep them easily accessible to the unit tests?
Did you tried to move this logic to the helper itself ? It can receive a varargs of SessionType to connect and keep them until you tell him to destroy them. It can maintains a Map&lt;SessionType, DatabaseConnection&gt; to give access to them.

/* WARN; This is not production ready. Validation and exceptions handling 
 * is missing and the "destroy" method can be improved.
 */
public class DatabasesHelper {
  private final Map&lt;SessionType, DatabaseConnection&gt; connections = new HashMap&lt;&gt;();

  public void prepare(SessionType... sessions) {
    for(SessionType session : sessions) {
      connections.put(session, this.DoLogin(session));
    }    
  }

  public void destroy() {
    for(SessionType session : sessions) {
      this.DoLogout(connections.get(session));
    }    
    sessions.clear();
  }

}


Please note that your methods names does not follow the Java, naming conventions. You should use lowerCamelCase for them.