4

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();
}
7

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.

  • Yeah, basically the whole later looks like this with the only difference the amount of parameters the methods have and forward. My main idea was, do the "good" programmers keep writing tests for every single class, however short/simple/easy it is. I have done the model so far, I will also do the db layer as all the magic happens there - I/O of data. Thank you for your answer :) – Nikola May 31 '13 at 10:39
  • Having a 100% code coverage of tests is a feather in your project's cap, but you'll spend a lot of time on getting coverage for that last 30 or 40 percent that. That less 30 percent is often boilerplate or property bag code that could be time better spent on more features or cleaning up the less trivial sections through further, deeper refactoring. Definitely thoroughly test any 'magic' code you write. – JustinC May 31 '13 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.