I’m working on a play app and want to improve my test coverage before going too far with the project.
So far I have quite good test coverage on the model layer, but the controllers tend to get a lot of logic in them (accumulating stuff and calculating from the model results).
Does anyone have a suggestion on how to structure my application so that all parts are easily testable? I could of course refactor all the logic out of the controller and only have one call to a different class for each controller method and then only use the render methods in the controller, but it seems a bit drastic.
Is there a way I can look at the results after a method has been run in the controller, e.g
public static void controllerMethod(String a){
String result = doSomething();
String calcResult = calculateSomething(a);
render(result, calcResult)
}
How can I test that after this method the expected result and calcResult is what I want it to be in a unittest?
All the examples on the play site is about testing the model layer. I know I can run Functional tests towards the controller, but that is a bit to shallow for what I want to test.
If you have any suggestions about how I can structure my classes in order to have an easily testable application, please let me know.
Controllers task should be (mostly) control of the flow of the application (which page to show next). For that you can use the selenium tests in Play.
If you find yourself adding a lot of login in the controller, either move it to methods in the related model (if applicable) or create a middle layer (ala Services) that manages the complex parts. They will be easier to test than Controllers, and will let you reuse the logic on several controllers (for example, acess via browser of REST API call)