1

Say I have a Service Layer method like this:

public void TestMethod()
{
   DomainObject domainObject = new DomainObject();
   domainObject.TestMethod();
}

TestMethod looks like this in the Domain Layer:

public void TestMethod()
{
   TestMethod1();
   TestMethod2();
   TestMethod3();
}

Is it bad practice to Unit Test this Service Layer method? The reason I ask is because the Domain Object.TestMethod calls three other methods. My reading tells me that you should only test one method per unit test.

w0051977
  • 7,081

2 Answers2

6

You should absolutely test this service layer method. Otherwise, how do you know if it works?

Even if you have individual unit tests which guarantee that each of TestMethod1(), TestMethod2() and TestMethod3() works as expected, this does not guarantee TestMethod() works as expected. E.g. it might call the methods in the wrong order, or this set of methods might be be the wrong methods to call in order to fulfill its purpose.

It is sometimes stated that a unit test should only test a single method, but this just refers to a single method at the top level in the unit test. Certainly the tested method might call other methods in turn.

JacquesB
  • 59,334
2

What are you testing? You're testing TestMethod. If the implementation of TestMethod changes, you still want to test this. Your test should be as implementation-independent as possible, so testing this method is important.

My reading tells me that you should only test one method per unit test.

I would suggest you only want to test one invocation/method of your component under test per method, because otherwise your unit test ends up testing a lot, and if your first invocation fails, you won't get a chance to even invoke the subsequent methods. How that method is implemented, however, should be opaque to the test. If you change that implementation in the future, your test should remain the same, and hence confirm your refactoring did not change the behaviour of that method unexpectedly.

Brian Agnew
  • 4,676