Unit Testing vs Integration Testing: Fundamentals and Mocking Quiz

Test your understanding of the key differences between unit and integration testing, explore when to use mocks and stubs, and learn how to write reliable, isolated tests. This quiz covers basic testing concepts and best practices for isolating dependencies in software tests.

  1. Identifying Unit Tests

    Which of the following best describes a unit test?

    1. A test that validates the user interface and visual elements.
    2. A test that checks the functionality of a single function or method in isolation.
    3. A test that verifies the combined behavior of several integrated components.
    4. A test run on the deployed application in the production environment.

    Explanation: A unit test focuses on verifying the correctness of a single function, method, or small module without involving other system components. In contrast, testing multiple components working together refers to integration testing. Testing user interfaces relates to UI testing, while testing in production is often called acceptance or smoke testing. Unit tests are meant to be fast, simple, and isolated from other parts of the code.

  2. Purpose of Integration Testing

    What is the primary goal of integration testing in software development?

    1. To test the speed and performance of the whole application.
    2. To verify that multiple units or modules work together correctly.
    3. To ensure that individual units function as expected on their own.
    4. To catch spelling and grammar errors in the codebase.

    Explanation: Integration testing focuses on examining how different units or modules interact, ensuring that combined behavior meets expectations. While unit testing looks at small pieces in isolation, integration tests reveal issues at connection points. Testing speed and performance are goals of performance tests, not specifically integration tests. Catching spelling and grammar errors is a job for linters or code reviews, not integration testing.

  3. When to Use Mocking

    In which scenario is mocking most useful during unit testing?

    1. When you need to replace a slow or unavailable dependency, such as a database.
    2. When refactoring code without changing any external behavior.
    3. When validating the look and feel of a graphical user interface.
    4. When deploying the application to the production environment.

    Explanation: Mocks are used to simulate dependencies like databases or APIs that might be slow, unreliable, or hard to control during tests, making unit tests fast and deterministic. GUI validation does not involve mocks in this context. Deployments to production are unrelated to the use of mocks. While refactoring might benefit from tests, mocking is not specifically a focus unless external dependencies are involved.

  4. Stubs in Testing

    Which statement accurately explains the function of a stub in testing?

    1. A stub automatically writes tests based on code structure.
    2. A stub checks for memory leaks in the running system.
    3. A stub monitors and asserts how a dependency was used during testing.
    4. A stub supplies predefined responses to calls made during the test.

    Explanation: Stubs are dummy components providing fixed responses, allowing tests to proceed without involving real dependencies. Unlike stubs, spies monitor and assert behaviors, not provide responses. Memory leak detection is unrelated to stubs, and stubs do not generate tests automatically from code structure.

  5. Deterministic Tests

    Why is it important for unit tests to be deterministic?

    1. So tests select random data to increase coverage.
    2. So testers can manually adjust results after each run.
    3. So tests always produce the same result given the same input.
    4. So tests can fail or pass unpredictably.

    Explanation: Deterministic unit tests ensure consistency and reliability by producing the same output for identical inputs, improving developer trust and simplifying debugging. Introducing randomness, as in option two, reduces test reliability. Failing or passing unpredictably, from option three, undermines the purpose of testing. Manually altering test results, as suggested by option four, is not a sound testing practice.

  6. Unit Test Isolation

    How does using mocks or stubs help isolate a unit test?

    1. They prevent real dependencies from influencing the test outcome.
    2. They add more external services to the test environment.
    3. They ensure the code handles security vulnerabilities.
    4. They verify that the user interface is visually appealing.

    Explanation: Mocks and stubs replace real external dependencies, ensuring the unit under test is evaluated in isolation, free from side effects or failures in other components. Adding more external services, as suggested in option two, increases complexity rather than isolation. Visual appeal and security testing, mentioned in options three and four, are not related to the isolation of unit tests.

  7. Integration Test Example

    Which is the best example of an integration test?

    1. Testing for spelling mistakes in documentation files.
    2. Testing two connected modules to verify they work together, such as a payment processor calling a notification service.
    3. Testing if a constant value exists in a configuration file.
    4. Testing a single function that calculates tax based on input.

    Explanation: Integration tests assess the correct functioning of connected modules, like a payment processor integrating with another service, which unit tests do not cover. Testing a single function is unit testing, not integration. Checking for spelling mistakes or constants in files is not related to integration testing.

  8. Choosing the Right Test Type

    If a developer wants to ensure that an algorithm produces correct results regardless of external services, which type of test is most suitable?

    1. User acceptance test
    2. Performance test
    3. Unit test with mocked dependencies
    4. Syntax checker

    Explanation: A unit test with mocks isolates the algorithm from external influences, so only the algorithm's correctness is evaluated. User acceptance testing focuses on meeting user needs, not algorithm logic. Performance tests measure speed, not correctness. Syntax checkers detect code errors or typos, not correct output.

  9. Test Double Terminology

    Which of the following is NOT a common type of test double used in unit or integration testing?

    1. Fake
    2. Mock
    3. Compiler
    4. Stub

    Explanation: Compilers are tools that convert source code to executable programs, not test doubles. Stubs, mocks, and fakes are all types of test doubles used to mimic or replace real components during testing. Only option one stands out as unrelated to the topic of test doubles.

  10. Drawbacks of Not Mocking

    What is a likely drawback of not using mocks or stubs when unit testing a function that relies on a network call?

    1. The function will always be correct.
    2. The test cannot be written at all.
    3. The test will produce random data automatically.
    4. The test might be slow, flaky, or fail due to network issues.

    Explanation: Without mocks or stubs, tests that depend on network calls can be unreliable and slow, since they rely on external factors like network availability. It does not guarantee the function is correct, nor does it cause the test to produce random data or completely prevent test creation. Using test doubles improves reliability and speed.