What is the purpose of a "stub" in software testing?

2025

What is the purpose of a "stub" in software testing?

  1. A.

    To simulate the behavior of a missing or incomplete module

  2. B.

    To generate test data for a module

  3. C.

    To analyze code coverage during testing

  4. D.

    To identify performance bottlenecks in a module

Attempted by 131 students.

Show answer & explanation

Correct answer: A

Answer: To simulate the behavior of a missing or incomplete module

Explanation: Stubs are simple, temporary implementations used in testing to stand in for dependent modules that are not yet developed or are unavailable. They provide fixed or pre-defined responses so the module under test can be exercised in isolation.

  • Allow early testing: Enable testing of a component before its dependencies are ready.

  • Isolate behavior: Help isolate the logic of the module under test by removing variability from dependencies.

  • Simplify setup: Reduce complexity of test environments by replacing external services with lightweight substitutes.

Contrast with mocks: Mocks often include verification of interactions and expectations, whereas stubs typically only provide canned responses without asserting how they were called.

Example: If a payment service is not available during testing, use a stub that returns a successful payment response for certain inputs so you can test order processing logic without the real payment system.

Explore the full course: Computer Science