what is mocking
creating simulated objects that mimic behaviour of real objects in controlled ways
purpose of mocking
1) break dependencies
2) to simulate behaviour
3) isolate components
ALL during unit testing
why do we mock
VERY IF NOT MOST IMPORTANT FLASHCARD EVERY SENTENCE IS IMPORTANT HERE
We mock because we want to break external dependencies (keeping test loosely coupled) whilst also allowing the unit test to remain as a safety net that provides quick feedback. This is because the external dependency may involve a database and need long data base accesses or someother thing that takes time to excute like io and the unit test would become slow. Also when we have units that depend on other units it may introduce sideefects ( ie in the case of database where a real access may change the state of database which we dont want to do in a test)
When to mock
1) break dependencies between layers and tiers of code
explanation of 1)
break dependencies - i can mock an object rather than being forced to instantiate an object of it which means high coupling ( makes unit tests quick especially in cases where external dependy is database, network…)
2) corner cases and exceptional behaviour
explanation of 2)
logic may depend on exceptional stuff like security or something so i can mock the behaviour and feed it predetermined results so i can test that the actual logic works without being too woried about exceptional stuff and corner cases
when to not mock
Replacing awkward 3rd party apis
responsibilities which involve large amounts of state or data - which could be more conveniently expressed in a native format
HERE YOU MAKE A JUDGEMENT CALL IF MOCKING IS MORE EFFORT THAN JUST HAVING EXTERNAL DEPENDENCY DO NOT MOCK
law of demeter
methods should only interact with closely related objects to reduce coupling
good tip ( instead of classes talking to each other use an interface)
what is dependency injection
dependencies are injected into an object at runtime rather than created internally, improving testability and reducing coupling.
dummy
Placeholder objects that do nothing but avoid null values.
- Example: Empty object for mandatory parameters.
ie when we created an empty librarymember that does nothing when testing issueto which required a mandatory library member
tip: presence needed but they do nothing
stub
so stub - test double that delivers indirect inputs and is only used in test scope
example: public class AlwaysFailingDispenserStub implements IDispenser { public void dispense(BigDecimal amount) throws DispenserFailed { throw new DispenserFailed(ErrorType.HARDWARE, “Failing dispense”); } }
test doesnt directly produce the failure -> failure comes from dispense.dispense ( output of stub method)
STUB ONLY USED TO SIMULATE SPECIFIC SCENARIO ( which is why its only used in test scope)
fake
A fake is a mostly working implementation of a real class.
It either extends the real class or implements the same interface, but it hacks performance by overriding or replacing slow parts, such as database access, with simpler in-memory logic.
mock
mock - objects with expectations . We tell the mock what values we want to return. In test we run the real logic and see if it matches
can keep track of no of times method on mock object been called
spy
Similar to mock but tracks method invocations during testing.
Advantages of mocking
disadvantages of mocking
coverage
Measures the extent to which the codebase is executed during testing.
Read
int add(int a, int b) {
return a - b;
}
This function may achieve 100% test coverage (statement, branch, or path coverage), but it is incorrect because it does not meet the requirements of addition.
COVERAGE ONLY SHOWS CODE WAS EXECUTED - DOESNT ENSURE LOGIC IS CORRECT
limitations of coverage