views:

99

answers:

3

I'm working on a legacy system that has uses stored procs, business objects and DTO:s. The business objects and the DTO:s often have the same properties. When calling a method in the service layer that returns a DTO, many transformations are happening. Stored proc -> dataset -> business object -> DTO. If a new property is added, it sometimes happens that a developer forgets to add code that moves it from one layer/object to another.

In some parts of the system I solved this by using AutoMapper which will automatically project properties with the same name.

My question is for the other parts. Can I somehow write a unit test that checks if every property in an object has been set/given a value? That way I could write an integration test that calls our service layer and all the transformations have to be successful for the test to pass.

I guess the solution would involve reflection.

A: 

Yes, reflection would be the way to go.

It's probably best to perform the unit test against some mock objects, so you have a known value to test for.

richardtallent
A: 

Reflection is one way, but it has its caveats, if you set a property to its default value, you will not pick up on the fact it was set.

You can intercept with a real proxy and then listen on all property changes. See the code here for a base interceptor you can use. Note interceptors mean you need your object to be MarshalByRefObject which may not be something you want. So the other option is to tell your factory to wrap up the object before it returns it in the test scenario. Something that ninject or many other inversion of control libs will allow you to do.

Sam Saffron
True about the default values. We do not use an IoC container today but the need get bigger every day.
Jimmy
A: 

Maybe you could change your BO/DTO's to implement INotifyPropertyChanged Interface. This way you could setup some listener to tell your unit/integration test what properties were changed.

In listener you save the list of all changed properties and with reflection you could check in there are additional properties that are not in the list.

Petar Repac