Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing of DTOs

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.

like image 734
Shikarn-O Avatar asked Feb 25 '11 11:02

Shikarn-O


People also ask

Should you unit test DTOs?

@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.

How do you mock a DTO?

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.

How do you write a JUnit test case for POJO class?

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.

Can DTOs have methods?

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.


1 Answers

You should not unit test DTO's getters and setters, unless they contain some complex logic that requires testing.

like image 198
Luciano Fiandesio Avatar answered Sep 27 '22 01:09

Luciano Fiandesio