Test DTOs for default constructor
Monday, February 20, 2012 16:05:54
Here's a quick test method I put together to save me from myself. If you organize your data transfer objects in a single assembly, and use normal serializable classes, you need to ensure that you always have a default constructor. The compiler won't help you out with this one - you've got to do it yourself. Adding this to your unit test project will ensure that you don't accidentally put in a custom constructor without adding in a default one.
[Test]public void EnsureAllDTOClassesHaveDefaultConstructor()
{
foreach (var type in typeof(SomeDTO).Assembly.GetTypes())
{
if (!type.IsAbstract && type.GetConstructor(new Type[0]) == null)
{
Assert.Fail("Type {0} is missing the default constructor.", type.FullName);
}
}
}
Tags: programming
Comments:









