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?
-
1\$\begingroup\$ Is your intention really doing unit testing or do you do integration testing using a 'JUnit' \$\endgroup\$Timothy Truckle– Timothy Truckle2018年08月15日 13:56:27 +00:00Commented Aug 15, 2018 at 13:56
-
\$\begingroup\$ Yes, you're correct it's more integration testing using JUnit in this instance. \$\endgroup\$Meridian– Meridian2018年08月15日 14:03:17 +00:00Commented Aug 15, 2018 at 14:03
1 Answer 1
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.