MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter....
Transcript of MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter....
MOCK OBJECTS
SE2832- Introduction to Software Verification
Materials
▷ Github: https://github.com/dennis-classes/se2832-mocks▷ Mockito:
○ https://github.com/mockito/mockito○ http://site.mockito.org/○ org.mockito:mockito-junit-jupiter:2.18.3
▷ Tutorials: ○ http://www.vogella.com/tutorials/Mockito/article.html○ https://www.tutorialspoint.com/mockito/mockito_over
view.htm○ http://www.baeldung.com/mockito-verify○ http://www.baeldung.com/mockito-behavior
Outline
▷ Testing Difficulties▷ Mocks
Outcomes
By the end of the class, you should be able to:
▷ To explain the concepts of test doubles.▷ To understand the relationship between dummy
objects, fake objects, stubs, spies, and mocks.▷ To explain the Mock Object testing pattern.▷ To be able to write a simple tests using Mockito
1.Testing Troubles
What do we do when testing gets hard?
twitter4jLet’s take a look at this simple Twitter client that uses twitter4j as the Twitter API client.
https://github.com/se2832/se2832-mocks
Complex SystemsThis can be very problematic, fortunately we have ways to deal with this.
3.Mocking
What are test doubles and how to we test with them?
test doublesAre ways for us to simplify and increase controllability into our testing.
Test Doubles
▷ Test double - a test double is a stand-in for a real, production object. Similar to a stunt double.
▷ Gerard Meszaros in his 2007 book: xUnit Test Patterns: Refactoring Test Code first coined the term and suggested the following categorization of test doubles:
http://googletesting.blogspot.com/2013/07/testing-on-toilet-know-your-test-doubles.html
Test Doubles
▷ Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
▷ Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production.
▷ Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
Test Doubles
▷ Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
▷ Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive.
Colloquially
It’s common to refer to all of these as “mocks”.
However, that is an abuse of the term, and precision, especially in language, is always preferred.
Mock Object Pattern
Let’s take another look @twitter
next meetingInput DomainAnalysis