Given the following SUT, would you consider this unit test to be unnecessary?
**edit : we cannot assume the names will match, so reflection wouldn’t work.
**edit 2 : in actuality, this class would implement an IMapper interface and there would be full blown behavioral (mock) testing at the business logic layer of the application. this test just happens to be the lowest level of testing that must be state based. I question whether this test is truly necessary because the test code is almost identical to the source code itself, and based off of actual experience I don’t see how this unit test makes maintenance of the application any easier.
//SUT public class Mapper { public void Map(DataContract from, DataObject to) { to.Value1 = from.Value1; to.Value2 = from.Value2; .... to.Value100 = from.Value100; } } //Unit Test public class MapperTest() { DataContract contract = new DataContract(){... } ; DataObject do = new DataObject(){...}; Mapper mapper = new Mapper(); mapper.Map(contract, do); Assert.AreEqual(do.Value1, contract.Value1); ... Assert.AreEqual(do.Value100, contract.Value100); }
It is not excessive. I’m sure not sure it fully focuses on what you want to test.
‘Under the strict definition, for QA purposes, the failure of a UnitTest implicates only one unit. You know exactly where to search to find the bug.’
The power of a unit test is in having a known correct resultant state, the focus should be the values assigned to DataContract. Those are the bounds we want to push. To ensure that all possible values for DataContract can be successfully copied into DataObject. DataContract must be populated with edge case values.
PS. David Kemp is right 100 well designed tests would be the most true to the concept of unit testing.
Note : For this test we must assume that DataContract populates perfectly when built (that requires separate tests).