Lecture 7-Mockito Flashcards

1
Q

What are the 6 steps of a mock?

A

1.Create a mock
2.Verify
3.Stubbing
4.Iterating
5.Argument Captor
6.Spy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What is a mock?

A

An object with the ability to :
(a) have a programmed expected behaviour
(b) verify the interactions occurring in its lifetime–> use of mocking framework

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is the Verify step so important?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens in Stubbing?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is important about Stubbing(3)?

A
  1. Once stubbed, the method will always return a stubbed value, no matter how many time it is called.
  2. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do we verify that the interactions happened in a particular order(3)?

A

-Check that verify of call of barcode happens before showline
-Use the InOrder class
-Tell it which classes will be involved

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does Spying do?

A

When using the spy, the real methods are called. (unless it was stubbed)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why do we want to avoid Spies?

A

They have lots of Gotchas!!!
–> Use only when you have legacy code that you can’t completely Mock out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the 5 precautions during mock testing?

A

-Do not mock types you don’t own
-Dont mock value objects
-Dont mock everything
-Mock interfaces, not concrete classes
-Use Integration tests after

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the 4 advantages of mock testing?

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly