How to unit test WCF service?

We have a whole bunch of DLLs that give us access to our database and other applications and services.

We wrapped these libraries with a thin WCF service layer, which our customers then consume.

I'm a little unsure of how to write unit tests that check only the WCF service level. Should I just write unit tests for DLLs and integration tests for WCF services? I would appreciate any wisdom ... I know that if my unit tests really go to the database, they will not actually be real unit tests. I also understand that I really don't need to test the WCF service host in unit test.

So I'm confused exactly what to test and how.

+18
unit-testing wcf
Sep 01 '08 at 2:13
source share
3 answers

The consumer of your service doesn’t care what’s under your service. To really test your level of service, I think your layer should go to the DLLs and the database and write at least CRUD .

+4
01 Sep '08 at 2:27
source share

If you want to unit test your WCF service classes, make sure that you create them taking into account free communication, so you can scoff at each dependency, because you want to test the logic inside the service class itself.

For example, in the service below, I exit my data access repository using "Injection Dependency Injection".

Public Class ProductService Implements IProductService Private mRepository As IProductRepository Public Sub New() mRepository = New ProductRepository() End Sub Public Sub New(ByVal repository As IProductRepository) mRepository = repository End Sub Public Function GetProducts() As System.Collections.Generic.List(Of Product) Implements IProductService.GetProducts Return mRepository.GetProducts() End Function End Class 

On the client, you can mock the WCF service itself using the service contract interface.

 <TestMethod()> _ Public Sub ShouldPopulateProductsListOnViewLoadWhenPostBackIsFalse() mMockery = New MockRepository() mView = DirectCast(mMockery.Stub(Of IProductView)(), IProductView) mProductService = DirectCast(mMockery.DynamicMock(Of IProductService)(), IProductService) mPresenter = New ProductPresenter(mView, mProductService) Dim ProductList As New List(Of Product)() ProductList.Add(New Product) Using mMockery.Record() SetupResult.For(mView.PageIsPostBack).Return(False).Repeat.Once() Expect.Call(mProductService.GetProducts()).Return(ProductList).Repeat.Once() End Using Using mMockery.Playback() mPresenter.OnViewLoad() End Using 'Verify that we hit the service dependency during the method when postback is false Assert.AreEqual(1, mView.Products.Count) mMockery.VerifyAll() End Sub 
+7
Sep 01 '08 at 2:26
source share

It depends on what the WCF thin service does. If it is really thin and there is no interesting code there, do not worry about it, checking it. Do not be afraid not to unit test if there is no real code. If the test cannot be at least one level simpler than the code under the test, do not worry. If the code is dumb, the test will also be dumb. You do not want to have more silent code for support.

If you have tests that go all the way to db, then great! This is even better. This is not a "true unit test?" No problem.

+7
Sep 01 '08 at 19:19
source share



All Articles