I am writing an app in Java Spring which consists of layers. The responsibilities of them are:
- REST/Controller layer - handle incoming requests, user authentification, parsing request bodies and communicating with Service layer according to the request
- Service layer - mostly deal with creating/editing/deleting associations between and states of Entities. Communicates with DAO layer and "Utility"
- DAO layer - persistence, will save/delete/find requested entities
- Entity/Object? layer - object representing the domain of the app, rather dumb functionality, mainly for keeping track of associations
- "Utility" layer - not really a layer, provides services like checking for permissions between objects (Object A wants to add object B to object C, for that it needs permission XYZ...) etc
I have been writing some tests while developing and now that I have thought about it, they have been kind of integration tests. The general flow of the tests can be described as:
- Call serviceX to create object A
- Call serviceX to create object B
- Call serviceY to do something with objects A and B
- Call serviceX to report state of objects A and B and check if everything is correct
In each step the services called the DAO layer and saved/created/fetched objects which were then manipulated and the test checked if the manipulation was successful and as expected.
I am thinking about how exactly should a proper unit test look. I understand that I should mock every component the tested service depends on so that I can 'isolate' the behavior I want to test. However, most of the methods of services do not actually "return" anything, they only manipulate the entities.
The question I have is then - does it always make sense to Unit test layers/services of an app, or is it acceptable to test them as a whole?
Appendix
Example of one of the methods. This one is responsible for adding a new user to a group. Adding the member has to be done by a manager of the group. So the logic goes:
- Get parameters - manager, group, newUser
- If any of parameters are null, throw exception
- Call
permissionHandler
service and check if themanager
user has enough permissions ingroup
to complete this action - Check if
newUser
already exists in the group. If yes, throw exception. - If everything ok, call
groupMemberHelper
service to create a newGroupMember
object representing the association betweenuser
andgroup
(and some more info about the association) - Add this
GroupMember
object to the group - For all "items" the group already owns, call a helper service that creates a link between those items and the newly added user.
To test this method I would keep track of the group
, newUser
and all objects that this method affects and check if the state "before" and "after" are what I expect them to be.
I can't think of another way to test it without relying on the functions of the called services. Am I wrong?
Aucun commentaire:
Enregistrer un commentaire