Foundation of Unit testing
Testing paradigm
Unit of work
Organizing a unit test
Testing with dependencies
Test doubles - Fakes, Stubs, Mocks, Dummies and Spies
Examples
2. About Techtalks
To build a community for
collaborative and continuous
learning
Technology trends
Tools and techniques
Software engineering practices
Ways of working
Tech careers
3. About me
Technical agility coach with Singapore Airlines
Worked in Investment banking, Fintech and Software services
Talk about Software craftsmanship, quality and agility
Trainer for Agile Product and Delivery management (APM), Product
Ownership(APO), Facilitation(ATF) and Foundation (ICP) with ICAgile
Co-founded TechTalks in 2016
4. Agenda
Foundation of Unit testing
Testing paradigm
Unit of work
Organizing a unit test
Testing with dependencies
Test doubles - Fakes, Stubs, Mocks, Dummies and Spies
Examples
6. What is a unit test?
Validates the expectations with the code
7. Why unit tests?
Builds developer confidence in code
Makes future changes easier
Helps to improve software quality.
Helps to understand the system behaviour.
Strengthens deployability of the code
8. Cost of automated testing
Reference: xUnit Test patterns Gerard Meszaros
9. How big is a unit?
Instantiating an object?
Setting field values?
Or A CRUD operation?
11. Terminology
SUT/CUT System under test or Code under test
Fixture The environment to run the tests on the SUT/CUT
Pre test conditions
Post test cleanup
DOC depended-on component
12. Principles for unit testing
Deterministic
Automated
Quick to run
Should not fail when codes internal structure changes
Should fail when the behavior of code changes
Cheap to read, write and change
Tests should reduce (and not introduce) risk.
Tests should be isolated and not dependent on each other
15. Code smells with Fixtures
Duplication
Hurts readability and understandability
Hard to maintain
16. Better Fixtures
Explicit extraction
Extract methods with fixture and teardown code
Call method in each relevant test case
Implicit reference
@Before and @After annotated methods for setup/teardown
18. Organizing a unit test case - AAA pattern
Arrange
Bring SUT to a desired state before
testing
Act
Act on the SUT by executing the
methods
Assert
Validate the output against
expectations
20. Test pyramid
E2E Testing
Integration testing
Unit testing
Each layer indicates a levels of
granularity for testing
The lower levels focus on
isolated code behaviors
Higher levels focus on
component integration and
overall system
Unit testing is cheaper and E2E
testing is costlier in terms of
Time to run
Time to change
Flakiness
22. Unit Testing vs Integration Testing
Characteristics Unit Test Integration Test
Interface Works independently of real
interface (File system, Database,
API)
Depends on interface
Time Quick to run Time consuming operation
Reliability Very reliable since meant to be
isolated
Flaky at time depending on
environment stability
Target Tests behavior of the code Tests behavior as well as
interactions between objects
Environment Environment independent Environment dependent
27. Time period of the day
A class with static
method that returns
time period of the day
Returns values
Morning, Afternoon
or Evening based on
LocalDateTime
29. Challenges
System time (Data source)
tightly coupled within
TimeOfTheDay class
Cant reuse this class
with any other source of
date/time
It violates SRP Fetches time
and processes it
Unit test cannot be
deterministic
42. Stub
A filler object to satisfy behavioral needs of code through
canned responses. Used as an alternative to expensive
integration testing for testing dependency responses
46. Fake
A Fake object has a working implementation and behavior.
Though meant for testing purpose and not suitable for
production. For instance, using in-memory database instead of
connecting to real database instance for operations
48. Scenario Invalid cart order
Adding order to cart
When item name is empty or when quantity is not greater than ZERO
Dont call inventory service
Dont add to cart
49. Spy
Spy is a stub that also records the interaction with caller
objects
56. Mockito mock
Alternative for expectation
when(inventoryService.reserve(anyString(),anyInt())).thenReturn(ItemStatus.RESERVED);
Alternative for verification
verify(inventoryService).reserve(cleaningBrushes.getItemName(),cleaningBrushes.getQuantity());
57. Mockito mock facts
Its a test double, unless you want to call real method
Stubbing is integrated with then methods
58. Mockito Spy
Represents the concept of partial mock
It calls real method, unless you want to mock
60. Conclusion
Unit testing helps to keep code maintainable and understandable
Helps to quickly validate the code expectations and hence ensuring
quality
Test double strategies help to replace the behaviors of DOC and run
tests in isolation