Mocking
Session 5
Oded Lachish Room: Mal 407 Visiting Hours: Wednesday 17:00 to 20:00 Email: oded@dcs.bbk.ac.uk Web Page:http://www.dcs.bbk.ac.uk/~oded/Tools2011/Tools.html
1
Mocking
2
Why do we need to mock?
Recall:
• Programming begins when most if not all of the code has yet to be written. Problem: •How can we do anything more than trivial testing? Solution: 1. Do something to the classes not yet implemented Why not? Some of it may not be yours, waist of time
3
So we need to mock?
What do we want?
• Simple •Fast • Easy to learn How: • Mockito ‐ framework for creating mock objects
Remember there are other options such as: Jmock, EasyMock,...
4
Adding JARs
5
Downloading Mockito
6Parameter Passing
7
Folder menu
lib
8
Copy mockito JAR to lib
If done externally then press F5 to (refresh)
9
Adding to Build Path
Not enough to add to lib. Tools need to know where it is located
10
After adding to Build Path
Now it also appears in referenced libraries.
11
Simple example
12
Something class
• We start by actually comparing a real object with a mocked one.
13
Junit test with mocking
From Mockito
14
After Running test
The mock object assert failed
15
Mockito syntax
• Something mockObject =mock(Something.class); Mock an object of type Something • when(mockObject.whatIsYourName()).thenReturn("Block").thenReturn("Rock"); When the whatIsYourName method of mockObjectis called for the first time return “Block”. When the whatIsYourName method of mockObject is called for the second time return “Rock”. Any later calls to whatIsYourName method of mockObject will return “Rock”.(try it on your own)
16
Why do we need this?
• We can just write code that returns this in the original methods.
I don’t think so!
• Because of Mockito we can avoid adding code were we don’t ...
Regístrate para leer el documento completo.