1

Suppose I have a template method template() which calls m1() and m2(). I have tests for m1 and m2. Now while testing template() should one use mocking to check if m1 and m2 were correctly called or test its API or both.

I feel testing to check the algorithm of template (using mocking) is low level and is almost like having a check against each line of source code.

I would like to hear what others think about this.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Susanta
  • 319
  • 2
  • 11

1 Answers1

0

I like to perform two types of testing: isolation and integration.

My isolation testing involves unit testing classes by mocking out all interactions with other classes. Using a mocking framework like Powermock, I can mock out all object interactions as well as verify that all of the defined (and not defined) interactions occurred during each test. These isolation tests are the low level tests that can give you the code coverage metrics your team expects. Continuous integration tools like Hudson can be used to automate the running and metrics gathering of your isoloation unit tests.

Integration testing involves running flows that invoke multiple classes. I like to build automated suites that test happy path scenarios as well as some error scenarios. However I don't build and maintain suites that touch every line of code in the app. It's a cost and risk based decision. If you can automate your integration tests using a tool like fitnesse, you can also use your integration tests as a smoke test for your development environment, running it throughout the day as needed to make sure your app and environment are up and running with no issues.

I think this is a very good topic to think about. I don't think there's a single right answer on this as there are a lot of factors to consider when making decisions on your testing approaches.

Scott
  • 1,263
  • 1
  • 12
  • 23
  • When we test interactions (using a mocking framework to check expectations) are not our tests getting tied to the implementations? Whereas if we test at the API level, i.e., whatever the javadoc promises, then implementation is free to change. I can see the value of checking expectations for publish-subscribe scenarios but I am not so sure on the template method. – Susanta Mar 23 '12 at 20:22