views:

36

answers:

2

Hi,

I’m using SharpArch with SharpArch.Contrib’s [Transaction] attribute. The Transaction attribute is added to an application service method and if an exception is thrown during that method then any changes to any domain objects are rolledback. From all appearances this works well.

However, I’m writing NUnit tests to confirm that exceptions are being thrown when appropriate (invalid state, security errors, etc) but I also want to confirm that the Transaction attribute is present and doing its job to rollback the changes. Is there any way I can do this?

I do trust that SharpArch.Contrib’s Transaction attribute is solid code, but some future programmer could accidentally remove the Transaction attribute from a method or disable it during testing which would not be caught by the unit tests. Am I being overly cautious?

Thanks
Dan

A: 

I think testing the transactionality of your code makes perfect sense. I would do it on integration level, when you connect to the real database - then it is easy to generate wrong data, catch the exception and assert that there were no changes done in the database.

I am not sure how you would test it on a unit test level.

Grzenio
I agree with Grzenio, verifying that your transaction handling is correct is very good. But I think you should test it without testing the actual implementation. Test the business case and verify that no data was saved if an exception was thrown, but that should be valid independent of framework or code to accomplish it.
HAXEN
+2  A: 

I think you're being a little paranoid, but that's OK :-)

If you trust the SharpArch code, then I wouldn't worry about the exception being thrown back to you correctly. Assume that it works and pick up any problems during integration or functional testing. Testing third party code only has value when you either don't trust it or you're trying to understand it.

On the other hand if you want to test for the presence of an attribute (i.e. validate that the method has the appropriate attribute on it) then you can write a test that uses reflection to inspect the method signature and do some assertions. It's not too hard to do - you just use the methodinfo object to work out the attributes on the method and scan for the TransactionAttribute.

Richard Banks
Totally. Don't test the libraries. Test what you do with them.
Andres Jaan Tack
Thanks. Fortunately the SharpArchContrib's Transaction attribute can be applied to the class level, and I apply a marker interface to the application services so they can be setup in the IoC container. This makes it easy to use reflection to test if the service classes that the attribute.
Dan