views:

57

answers:

2

I'm currently working on what I would call integration tests. I want to verify that if a WCF service is called it will do what I expect.

Let's take a very simple scenario. Assume we have a contract object that we can put on hold or take off hold. Now writing the put on hold test is quite simple. You create a contract instance and execute the code that puts it on code.

The question I have comes when we want to test the taking off hold service call. The problem is that putting a contract on hold can be actually quite complicated leading to various objects all be modified. So usually I would use the Builder pattern and do something like this..

var onHoldContract = new ContractBuilder().PutOnHold().Build();

The problem I have with this is now I have to pretty much replicate a large part of my put on hold service. Now when I change what putting something on hold means I have two places I have to modify.

The other option that immediately jumps out at me is to just use the put on hold service as part of my test setup but now I'm coupling my test to the success of another piece of code which is something I don't like to do since it can lead to failures in one spot breaking unrelated tests elsewhere (if put on hold failed for example).

Any other options I'm missing out here? or opinions on which method is preferable and why?

A: 

You could take a look at using a mocking framework.

Mocking frameworks allow you to create "mock" objects that can take the place of your real objects and respond in specific ways when used in tests. This would allow you just to create your artificial "on hold" contract object directly and then pass it to your "take off hold" service. You would define specific behaviour on the mock that could be validated so it acted correctly while being taken off hold.

You didn't mention your language, but all of the major languages have mocking frameworks available. For .net, some examples would be NMock of Rhino mocks. For Java, a popular one is Mockito. You'll find loads more if you google for one in whatever language you are using.

Simon P Stevens
As I mentioned I am doing integration tests which makes mocking things out pretty pointless.
ShaneC
Just because its an integration test doesn't mean a mock object can't be used. You still need to test that your WCF service call integrates with the tiers beneath it (assuming n-tier architecture). I'm not saying you should mock your tier boundaries as you would in a true unit test, just your business object.If that isn't what you want you don't really have any other options. We have a similar test setup where we use other service calls to get objects setup. You just have to accept the unrelated failures problem. The very nature of integration test suggests testing the whole system anyway.
Simon P Stevens
Your last comment is the kind of argument I'm looking for.
ShaneC
@ShaneC. Haha, perhaps I should edit my answer down to just that sentence.
Simon P Stevens
A: 

The Mock framework is a good option for unit testing. But for what I understood you are actually doing integration at this stage right? So you're calling your WCF service as a black box (from a client point of view). I'm assuming that when you're executing your On Hold operation, you have to do some persistance on a repository (Database, XML files, etc).

In this case then, the mock framework only at the testing side will not help you much, because in order to test the Off Hold operation, you need a On Hold object in the proper state, including repository entries etc. Seems to me that the only way to do this without having to reinvent the wheel is to use the service to put it On Hold first. But if you're admant to decouple then, then you will need to setup the environment and this means code duplication (I actually had to do this on some integration test scenarios - what I would do was to setup it on the start of the test run).

Notice that your unit tests would be on a lower level, in the implementation of the service itself, and there you should be decoupling it as much as possible - this is where I would be applying the Mock Framework.

I hope this helps.

Wagner Silveira
At least you read my question ;) Yeah I can't come up with any other options either and it seems like just using the code that causes the state I want to occur is a lot easier then replicating the work elsewhere.
ShaneC