16

I've got a bunch of methods in my application service layer that are doing things like this:

public void Execute(PlaceOrderOnHoldCommand command)
{
    var order = _repository.Load(command.OrderId);
    order.PlaceOnHold();
    _repository.Save(order);
}

And at present, I have a bunch of unit tests like this:

[Test]
public void PlaceOrderOnHold_LoadsOrderFromRepository()
{
    var repository = new Mock<IOrderRepository>();
    const int orderId = 1;
    var order = new Mock<IOrder>();
    repository.Setup(r => r.Load(orderId)).Returns(order.Object);

    var command = new PlaceOrderOnHoldCommand(orderId);
    var service = new OrderService(repository.Object);
    service.Execute(command);

    repository.Verify(r => r.Load(It.Is<int>(x => x == orderId)), Times.Exactly(1));
}

[Test]
public void PlaceOrderOnHold_CallsPlaceOnHold()
{
            /* blah blah */
}

[Test]
public void PlaceOrderOnHold_SavesOrderToRepository()
{
            /* blah blah */
}

It seems to be debatable whether these unit tests add value that's worth the effort. I'm quite sure that the application service layer should be integration tested, though.

Should the application service layer be tested to this level of granularity, or are integration tests sufficient?

sarnold
  • 102,305
  • 22
  • 181
  • 238
Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148

3 Answers3

14

I'd write a unit test despite there also being an integration test. However, I'd likely make the test much simpler by eliminating the mocking framework, writing my own simple mock, and then combining all those tests to check that the the order in the mock repository was on hold.

[Test]
public void PlaceOrderOnHold_LoadsOrderFromRepository()
{
    const int orderId = 1;
    var repository = new MyMockRepository();
    repository.save(new MyMockOrder(orderId));      
    var command = new PlaceOrderOnHoldCommand(orderId);
    var service = new OrderService(repository);
    service.Execute(command);
    Assert.IsTrue(repository.getOrder(orderId).isOnHold());
}

There's really no need to check to be sure that load and/or save is called. Instead I'd just make sure that the only way that MyMockRepository will return the updated order is if load and save are called.

This kind of simplification is one of the reasons that I usually don't use mocking frameworks. It seems to me that you have much better control over your tests, and a much easier time writing them, if you write your own mocks.

Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148
Robert Martin
  • 2,825
  • 1
  • 17
  • 6
  • 2
    Interesting take Uncle Bob. Seemed it ended quite abruptly though :) – Mr Moose Jul 13 '11 at 06:07
  • but there is probably also test for `Order.PlaceOnHold()`. So if now you decide that order with id 1 cannot be put on hold, you have 2 tests to fix – driushkin Jul 13 '11 at 07:59
  • @unclebob Thanks! I'm going to take this under consideration. @driushkin There is a test for `Order.PlaceOnHold()` for sure. However, whether an order can be placed on hold is entirely the concern of implementation the real Order class - the mock wouldn't have any effect on what should happen in the service method. – Josh Kodroff Jul 13 '11 at 13:49
  • @RobertMartin, I cannot understand why people are still using mocking frameworks, their usage brings nothing but unreadable tests. Regarding this, I believe there is something any of us (the ones who prefer simplicity) can do: ask mocking framework authors to stop the development of these harmful tools :) – turdus-merula May 04 '14 at 15:17
  • The advice to use hand crafted mocks makes this test much more readable. I've actually used this as an example of the benefits of doing such - see http://blog.shaunfinglas.co.uk/2015/08/why-i-dont-like-mocking-frameworks.html – Finglas Aug 04 '15 at 20:52
10

Exactly: it's debatable! It's really good that you are weighing the expense/effort of writing and maintaining your test against the value it will bring you - and that's exactly the consideration you should make for every test you write. Often I see tests written for the sake of testing and thereby only adding ballast to the code base.

As a guideline I usually take that I want a full integration test of every important successful scenario/use case. Other tests I'll write are for parts of the code that are likely to break with future changes, or have broken in the past. And that is definitely not all code. That's where your judgement and insight in the system and requirements comes into play.

Assuming that you have an (integration) test for service.Execute(placeOrderOnHoldCommand), I'm not really sure if it adds value to test if the service loads an order from the repository exactly once. But it could be! For instance when your service previously had a nasty bug that would hit the repository ten times for a single order, causing performance issues (just making it up). In that case, I'd rename the test to PlaceOrderOnHold_LoadsOrderFromRepositoryExactlyOnce().

So for each and every test you have to decide for yourself ... hope that helps.

Notes:

  1. The tests you show can be perfectly valid and look well written.

  2. Your test sequence methods seems to be inspired on the way the Execute(...) method is currently implemented. When you structure your test this way, it could be that you are tying yourself to a specific implementation. This way, tests can actually make it harder to change - make sure you're only testing the important external behavior of your class.

Marijn
  • 10,367
  • 5
  • 59
  • 80
  • 1
    These are really good points you bring up. I think I'll write the integration tests *first*, then see what possible added value unit tests would bring (e.g. that InvalidOperationExceptions are thrown when a bad order id is supplied). And you also bring up a good point about testing the implementation too tightly - the point is inputs and outputs, not implementation. – Josh Kodroff Jul 13 '11 at 13:41
4

I usually write a single integration test of the primary scenario. By primary scenario i mean the successful path of all the code being tested. Then I write unit tests of all the other scenarios like checking all the cases in a switch, testing exception and so forth.

I think it is important to have both and yes it is possible to test it all with integration tests only, but that makes your tests long running and harder to debug. In average I think I have 10 unit tests per integration test.

I don't bother to test methods with one-liners unless something bussines logic-like happens in that line.

Update: Just to make it clear, cause I'm doing test-driven development I always write the unit tests first and typically do the integration test at the end.

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73