I have a MVC which has this structure:
- ui
- controller
- db
- model
Basically the controller doesn’t really do much more than connection ui
with db
layer.
Do I need to provide JUnit tests for the controller (if the program’s point matters, it’s my semester project)?
Would that help me somehow as there isn’t much code – I basically take all the parameters and put them, shouldn’t this be covered by model layer tests?
Example methods I have in the controller:
Inserting:
public int createEmployeeJob(String jobName)
{
int result = -3;
try
{
EmployeeJob newEmployeeJob = new EmployeeJob();
newEmployeeJob.setJobName(jobName);
DBConnection.startTransaction();
IFEmployeeJob dbEmployeeJob = new DBEmployeeJob();
result = dbEmployeeJob.createEmployeeJob(newEmployeeJob);
DBConnection.commitTransaction();
}
catch (Exception e)
{
DBConnection.rollbackTransaction();
}
return result;
}
Retrieving:
public ArrayList<EmployeeJob> printAllEmployeeJobs() throws SQLException
{
IFEmployeeJob dbEmployeeJob = new DBEmployeeJob();
return dbEmployeeJob.printAllEmployeeJobs();
}
You need unit tests in proportion to the risk that the code in that class introduces an error.
Complex code is more risky than simple code. Long methods are more risky than short ones. Code that implements decisions is more risky than code that simply funnels values to somewhere else, etc. Therefore, as long as your controller layer does nothing but copy values from one place to another, it has the lowest priority for writing tests. But having tests is still better than not having them – even the simplest code can introduce errors, particularly since you believe it’s simple and check it last if something goes wrong.
Note that if the entire layer is so straightforward and repetitive that it really only repeats actions at another layer, the right thing to do is to have it auto-generated, write a test for the auto-generator, and then never worry about it again.
2