views:

126

answers:

2

I've got a set up with a number of layers:

  • Website
  • Application / Service
  • Domain (contains entities)
  • Persistence (contains repositories)

I'm testing the persistence layer in isolation OK using data created in memory from a stub object.

Now, Im thinking about testing my Website layer. I know I should be testing it in isolation which Im thinking means creating a stub for the Application layer object it uses but this stub would need its own set of in memory data, duplicated in the stub in the persistence layer and I dont want to do duplicate this and manage it.

So my question is should the subject under test always work with stub objects from the layer below in order to be isolated and do they normally have their own set of data? Or is OK for my web method under test to call a lightweight object in the Application Layer which calls the Persistence layer with stub data?

Thanks for your help. This feels like the last bit of the puzzle for me ...

+4  A: 

Ideally in unit testing each subject under test is isolated from its dependencies. You do not want to think your subject under test is broken because one of its dependencies broke and caused the subject under test to fail. If you test like this, you might spend a lot of time tracking down bugs in the wrong place.

Testing how things operate together is the domain of integration testing, not unit testing

Jason
Thanks. I really need to be reminded of this time and time again to getused to this way of working. Think Ive got it now
Steve Ward
A: 

Or is OK for my web method under test to call a lightweight object in the Application Layer which calls the Persistence layer with stub data?

If you do this, I wouldn't call the test a unit test in isolation anymore - if the test fails, where is the bug? - but an integration test. Don't misinterpret me, integration testing is fine too, it just has another purpose. But if your goal is to unit test the website layer in isolation, you should mock/stub direct dependencies.

Pascal Thivent