Testing Flashcards
JUnit, TestNG, Mockito, etc.
Type of tests
- Sanity Test Cases
- Smoke Test Cases
- Regression Test Cases
stub?
Stubs always replace an object that we cannot control with a test-only version that we can control. They always produce known data values
for our code under test to consume.
Stub vs Mock?
A “stub” is a simple, hardcoded implementation of a function or method that returns predefined data. Stubs are used to simulate parts of the system that are not yet implemented or are external dependencies. They don’t have any logic and are just there to make the code compile and run for testing purposes.
A “mock,” on the other hand, is a more complex testing object that simulates the behavior of a real object. Mocks are often used to verify interactions between the code being tested and the mocked objects. They can record method calls and their parameters to ensure that the code under test is behaving correctly.
So, in summary, stubs are used for providing minimal, predefined responses, while mocks are used for verifying interactions and behavior in testing.
test double?
This term encompasses: dummy, fake, stub, mock
Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ‘sent’, or maybe only how many messages it ‘sent’.
Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
Stub vs Mock Life cycle?
Test lifecycle with stubs:
Setup - Prepare object that is being tested and its stubs collaborators.
Exercise - Test the functionality.
Verify state - Use asserts to check object’s state.
Teardown - Clean up resources.
Test lifecycle with mocks:
Setup data - Prepare object that is being tested.
Setup expectations - Prepare expectations in mock that is being used by primary object.
Exercise - Test the functionality.
Verify expectations - Verify that correct methods has been invoked in mock.
Verify state - Use asserts to check object’s state.
Teardown - Clean up resources.