MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter....

16
MOCK OBJECTS SE2832- Introduction to Software Verification

Transcript of MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter....

Page 1: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

MOCK OBJECTS

SE2832- Introduction to Software Verification

Page 2: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

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

Page 3: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

Outline

▷ Testing Difficulties▷ Mocks

Page 4: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

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

Page 5: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

1.Testing Troubles

What do we do when testing gets hard?

Page 6: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

twitter4jLet’s take a look at this simple Twitter client that uses twitter4j as the Twitter API client.

https://github.com/se2832/se2832-mocks

Page 7: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

Complex SystemsThis can be very problematic, fortunately we have ways to deal with this.

Page 8: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

3.Mocking

What are test doubles and how to we test with them?

Page 9: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

test doublesAre ways for us to simplify and increase controllability into our testing.

Page 10: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

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

Page 11: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

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.

Page 12: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

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.

Page 13: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

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.

Page 14: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

Mock Object Pattern

Page 15: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

Let’s take another look @twitter

Page 16: MOCK OBJECTS - se2832.bcdennis.com€¦ · Mock Object Pattern. Let’s take another look @twitter. next meeting Input Domain Analysis. Title: MOCK OBJECTS Created Date: 4/24/2019

next meetingInput DomainAnalysis