Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking for integration tests

How does one mock the many dependencies needed for integration tests?

I use Mockito for my 'pure' unit tests. 'Pure' in this case means testing a single class, mocking all of it's dependencies. Beautiful.

Now come integration tests. Let's say in this case an integration test will test something like this:

  1. Message is put on a queue
  2. Message is 'processed'
  3. Response message is put on a response queue

Let's also say that the processing that happens in step 2 is serious stuff. It relies on lots of database interactions, on multiple external services, the file system, all kinds of things. There are also a lot of side effects that the flow will trigger, so I cannot simply ensure that the response is correct - I need to verify the side effects.

Each of these dependencies are wrapped by a single stateless service class, which makes them nice and mockable.

How are folks handling this?

I would love to use Mockito so that I could verify the side effects that the above flow will have. However, Mocktio's documentation (and to a large extent it's implementation) seems to fight strongly against using it in contexts other than 'pure' unit tests. I've tried to go this route, but

  • It's difficult to populate the stub data (as there's lots of it)
  • It's difficult to have Spring inject those stubbed instances into my beans
  • It's difficult to 'reset' the mocks so that I can verify a different set of interactions without clearing out the stubs.

EDIT

I know that I could handle the database issue with something like an HSQLDB instance, but there's still the issue of the external services. For repeatability I can't rely on those services being up, being in the state that I require, etc. The only option I see there is to mock them.

Whatdaya do?

like image 888
Roy Truelove Avatar asked Apr 12 '12 20:04

Roy Truelove


People also ask

What is mocking in integration testing?

Mocking in programming refers to an action of substituting a part of the software with its fake counterpart. Mocking technique is primarily used during testing, as it allows us to take out certain aspects of the tested system, thus narrowing the test's focus and decreasing the test's complexity.

Can we mock in integration testing?

Integration testing is a must if you want stable software. However, it's difficult to get your integration tests to call the real dependencies, especially when it comes to load tests. That's where API mocking can help.

When should you not use mocks?

The costs of mocking In my experience, there are 3 reasons to mock sparingly: Using mocks leads to violations of the DRY principle. Using mocks makes refactoring harder. Using mocks can reduce the simplicity of the design.


1 Answers

Great question.

It seems like you hit the limits of Mockito. Mockito is great if what you want to inspect object interactions.

What you want, though, seems to be observability (and controllability) at a higher level of abstraction. I'm afraid that the mocks or stubs you need for that should be carefully designed and hand-crafted.

At the unit level, these mocks can be nicely generated, by means of Mockito. At the integration level, this becomes much harder, and you will need purpose made testability interfaces.

like image 67
avandeursen Avatar answered Sep 21 '22 17:09

avandeursen