Lecture 7-Mockito Flashcards
What are the 6 steps of a mock?
1.Create a mock
2.Verify
3.Stubbing
4.Iterating
5.Argument Captor
6.Spy
What is a mock?
An object with the ability to :
(a) have a programmed expected behaviour
(b) verify the interactions occurring in its lifetime–> use of mocking framework
Why is the Verify step so important?
It allows us to determine what was passed to a mocked method by the method under test:
-asserts only check RETURNED values
-verify checks that a method is CALLED
What happens in Stubbing?
We return whatever value we code:
BY DEFAULT:
-return null
OR
-return a primitive wrapper value
OR
-return empty collection
Ex: 0 for int and false for boolean
What is important about Stubbing(3)?
- Once stubbed, the method will always return a stubbed value, no matter how many time it is called.
- Stubbing can be overridden–> common stubbing can go to fixture setup, but test methods can override it.
*Overriding stubbing is a potential code smell that points too much stubbing.
3.The order of stubbing matters.
How do we verify that the interactions happened in a particular order(3)?
-Check that verify of call of barcode happens before showline
-Use the InOrder class
-Tell it which classes will be involved
What does Spying do?
When using the spy, the real methods are called. (unless it was stubbed)
Why do we want to avoid Spies?
They have lots of Gotchas!!!
–> Use only when you have legacy code that you can’t completely Mock out
What are the 5 precautions during mock testing?
-Do not mock types you don’t own
-Dont mock value objects
-Dont mock everything
-Mock interfaces, not concrete classes
-Use Integration tests after
What are the 4 advantages of mock testing?
-Mock object isolates dependencies
-Mock test is faster –> uses mock databases + file system operations + external services
-No bad effects on third-party services
-Provides a limiting scope to make understanding of tests easier