Unit-testing – How Should I Unit Test A Data Transfer Object

unit testing

Consider the following sample C# Data Transfer Object (DTO):

public class MailingAddress
{
   public string StreetAddress {get; set;}

   public string City {get; set;}

   public string StateOrTerritory {get; set;}

   public string PostalCode {get; set;}
}

I can write unit tests to check for someone setting the various string members to null or empty strings. I could also make the setters private on all the fields so that it has to be constructed via a constructor.

I'm asking about this because the CodeCoverage tool is reporting 0% on all these DTO methods and I'm trying to figure out if there's some reasonable testing I might do here.

I have googled a bit and not come up with a lot. I also searched here but haven't found anything that seems to address this. If I've missed something please link it in the comments.

EDIT:

Someone helpfully suggested that my question might be answered by this question. The thing is that while it doesn't look like there's code being run for the various fields, there is, in fact default code there.

It wouldn't be a case of testing the language features. If someone modifies the default behavior of the get/set pairs then I should have unit tests around them to insure they still behave as expected.

Best Answer

This class is just a holder of data. It doesn't have any behavior to test. So, no, do not write test cases for this class.

However, your application should have functions that take an instance of this class as an argument. When writing test cases for those functions, you should be using a real instance of this data class. Since there is no behavior, you don't have to worry about external dependencies that might have side-effects. Using a real instance of the class instead of a test double will cause the lines of this class covered by the test suite.