I am in the process of learning to unit test. I have a ‘domain object’ that doesn’t do much apart from hold state (i.e. Employee without any business logic). It has a method SetDefaults() which just fills its state with reasonable values. A simple method.
But when I go to unit test this method all I can think of is to run the method then check that every field is what it should be. Like (in C#):
[TestMethod()] public void SetDefaultsTest() { Employee target = new Employee(); employee.SetDefaults(); Assert.AreEqual(employee.Name, 'New Employee'); Assert.AreEqual(employee.Age, 30); // etc. }
It feels wrong to duplicate the entire functionality of SetDefaults() within my test. Should I just leave this method untested? The problem is that I’d like a test to ensure that when new properties are added to the class they are also added to the SetDefaults() method.
Trivial getters and setters sometimes don’t have unit tests written for them. If that’s all that SetDefaults() does, it probably won’t hurt to skip it.
One thing you would want to consider testing, though, is that none of the set properties of the
employeeinstance are null after callingSetDefaults():This makes sense, since you really just care that they are set to some default value, and not so much that they’re a specific value.