Mocking is used to create objects that simulate the behavior of real objects. This is useful in testing to isolate the code being tested from external dependencies. For example, in a Java e-commerce application, Mockito can mock a payment gateway, allowing tests to focus on business logic without requiring actual transactions.