Oded Lachish Room: Mal 407 Visiting Hours: Wednesday 17:00 to 20:00 Email: firstname.lastname@example.org Web Page:http://www.dcs.bbk.ac.uk/~oded/Tools2011/Tools.html
Why do we need to mock?
• 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
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,...
Copy mockito JAR to lib
If done externally then press F5 to (refresh)
Adding to Build Path
Not enough to add to lib. Tools need to know where it is located
After adding to Build Path
Now it also appears in referenced libraries.
• We start by actually comparing a real object with a mocked one.
Junit test with mocking
After Running test
The mock object assert failed
• 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)
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 ...