Is it appropriate and necessary to test getters and setters?
I think they haven't any logic and they can't crash or throw any exceptions.
@sepehr It's relevant insofar as DTOs are not usually unit-tested because there is no behavior to test. DTOs don't have behavior because behavior isn't serializable, which is specifically the point of DTOs. So it's very common to hear and read that DTOs don't need testing.
If the DTO has logic in it you might like to mock out, I would move the logic out of the DTO. To create DTO, I would do this from text, either in the test itself, or from an external file. You could use JSon, but if you don't use that already I would use XMLEncoder/XMLDecoder.
Here we will see one complete example of JUnit testing using POJO class, Business logic class, and a test class, which will be run by the test runner. Create EmployeeDetails. java in C:\>JUNIT_WORKSPACE, which is a POJO class. get/set the value of employee's name.
DTOs normally are created as POJOs. They are flat data structures that contain no business logic. They only contain storage, accessors and eventually methods related to serialization or parsing.
You should not unit test DTO's getters and setters, unless they contain some complex logic that requires testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With