Integration Testing – Does It Use Mocks?

integration-testsmockingtestingunit testing

I am currently in a class for software testing where for our semester project, we have to perform multiple types of testing on it, such as unit testing and integration testing. For integration testing, the professor said to use mocks and mocking libraries (like EasyMock and Mockito) for our integration testing. I'm getting fairly confused though. Integration testing is testing outside classes, modules, services, etc. Why would mocks and stubs be proper to use in integration testing if you are testing a multiple classes and services?

Best Answer

If you have a piece of functionality that touches several external components, you might mock all but one to isolate and test a specific component. For example, suppose you have a function that calls a web service and then does something with a database based on the results. You could write three integration tests:

  1. a test that mocks the webservice call but involves real database connectivity.
  2. a test that makes a real webservice call but uses mock database connectivity.
  3. a test that makes a real webservice call and involves a real database connection.

If you run all three tests and 1 and 3 fail, there's a good chance that there might be a bug in your code that works with the database, since the only test that passed was the one using the mock database connectivity.

In general, integration tests don't use mocks, but I have done something like this on occasion.