views:

167

answers:

2

I'm little confused with Cactus and mock objects (jMock, Easy mock).

Could anyone please answer the following questions?

  1. When to use Cactus for testing?
  2. When not to use Cactus for testing?
  3. When to use mock objects for testing?
  4. When not to use mock objects for testing?
+2  A: 

Well, there is no greatly objective answer to this question, but mock objects are about testing behavior of your code, but cannot tell you if you are interacting with the container correctly.

Cactus runs your code in the container, so it is really more of an integration test, but what it tells you is that your code actually interacts with the container correctly (what it sends is legitimate, what it gets back is real).

So to sum up, a Mock tells you that if you wanted behavior x, you got behavior x. Cactus tells you that behavior x actually gets an expected result within the container.

Which one you use depends a lot on what you are trying to accomplish with your test. If you want to do more TDD, the mock approach is the way to go. You do separate prototyping to see if you know enough about how the container works to write the code, then you do the unit testing, and then you have integration tests/acceptance tests to make sure it all works.

If however you are trying to do more traditional unit tests, where you are hitting the code with different values trying to test edge cases and behavior, then doing it with mocks isn't going to tell you very much, since a big part of your container is missing.

I currently much prefer the Mock approach, but if I were to get back into Cactus it would be more to test the invariants about the container so that if we upgrade the container we know that nothing important changed in how the container works that should affect our code.

Yishai
+1  A: 

When to use Cactus for testing?

For integration testing (involving interactions with the container). That is or was the sweet spot for Cactus. These tests are typically coarse grained... and their execution is not very fast.

When not to use Cactus for testing?

For other tests than integration tests (and even for integration tests, it's been a while since I've seen Cactus tests).

When to use mock objects for testing?

When you want to unit test something in isolation of the dependent things. These tests are typically fine grained, and their execution is fast.

When not to use mock objects for testing?

When you want to write integration tests (i.e. to test interactions), functional tests, etc.

The Mock Objects vs In-Container testing page on the Cactus web site summarizes this pretty well.

Pascal Thivent