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.

migrated from stackoverflow.com May 16 '17 at 16:39

This question came from our site for professional and enthusiast programmers.

  • 6
    Consider this, if that test fails, how do you know which of the three methods it was testing failed? – Rowland Shaw May 16 '17 at 16:28
  • 2
  • 2
    I think your question is a bit confusing. Your service layer does not know how DomainObject.TestMethod is implemented. The implementation in your domain layer is completely irrelevant to whether you should unit test your service layer. – Vincent Savard May 16 '17 at 17:08
  • 1
    can you change the names of the methods and objects to something meaningful? I mean do you have a test called TestMethodTestMethod which Tests TestMethod – Ewan May 16 '17 at 17:16
  • 1
    @RowlandShaw If an exception is thrown, you see it in the stacktrace, if some intermediate value is wrong you see that when you quickly step through the code. – CodesInChaos May 16 '17 at 17:18
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.