3
\$\begingroup\$

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<DatabaseConnection> _connections = new ArrayList<>();
 @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?

asked Aug 15, 2018 at 8:10
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is your intention really doing unit testing or do you do integration testing using a 'JUnit' \$\endgroup\$ Commented Aug 15, 2018 at 13:56
  • \$\begingroup\$ Yes, you're correct it's more integration testing using JUnit in this instance. \$\endgroup\$ Commented Aug 15, 2018 at 14:03

1 Answer 1

1
\$\begingroup\$

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<SessionType, DatabaseConnection> 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<SessionType, DatabaseConnection> connections = new HashMap<>();
 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.

answered Aug 16, 2018 at 5:01
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.