Interface-Based Object-Oriented Design and Importance of Mock Objects
Mock Objects from Concept to Code
-
Upload
agile-institute -
Category
Documents
-
view
543 -
download
0
Transcript of Mock Objects from Concept to Code
![Page 1: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/1.jpg)
April 12, 2023 © Agile Institute 2012 1
Mock Objects: From Concept to Code
Rob Myersfor
Agile Development Practices East
07 Nov 2012
Café
![Page 2: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/2.jpg)
April 12, 2023 © Agile Institute 2012 2
![Page 3: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/3.jpg)
difficult to set up
A database.A complex object graph.
April 12, 2023 © Agile Institute 2012 3
Investor
AccountFund
Stock
StockStock
Account Fund
Stock
Stock
Stock
![Page 4: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/4.jpg)
expensive to test against*
The production database.Your personal credit card.A “password changed” confirmation
sent to the CEO of a large investment firm.
April 12, 2023 © Agile Institute 2012 4
* Each of these “Career-Limiting Moves” was actually witnessed by me
during the “dot-COM” era.
![Page 5: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/5.jpg)
chaotic
The database.A randomizer.The weather.
April 12, 2023 © Agile Institute 2012 5
![Page 6: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/6.jpg)
April 12, 2023 © Agile Institute 2012 6
The database.The network.The filesystem.Interplanetary radio signals?!
…slow…
![Page 7: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/7.jpg)
April 12, 2023 © Agile Institute 2012 7
Fast!
Reliable!
![Page 8: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/8.jpg)
April 12, 2023 © Agile Institute 2012 8
![Page 9: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/9.jpg)
from Martin Fowler et al
• Fakes provide reasonable facsimiles of the real thing, but are not quite robust enough for production (e.g., an in-memory database).
• Stubs provide scripted responses to calls. They may also record information about calls made during the test.
• Mocks are programmed by tests, creating expectations for potentially the entire specification of a call, including arguments.
April 12, 2023 © Agile Institute 2012 9
![Page 10: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/10.jpg)
classicists v. mockists
Hand-Crafted Mocks (“Stubs”)+ Simple & familiar.+ Black-box.+ Can do something clever (e.g., throw exception for
a special argument), or hold state (e.g., collect a buffered result for an assertion).
- Can get too complex if reused everywhere.
Dynamic Mocking Tools (“Mocks”)+ Great for interactions/protocols.+ Can be used in numerous variations or scenarios.- White-box: Can be too explicit about
implementation.April 12, 2023 © Agile Institute 2012 10
![Page 11: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/11.jpg)
April 12, 2023 © Agile Institute 2012 11
never an absolutist
![Page 12: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/12.jpg)
April 12, 2023 © Agile Institute 2012 12
Eart
hb
ou
nd
Invest
men
ts
![Page 13: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/13.jpg)
April 12, 2023 © Agile Institute 2012 13
Create a StockQuote for the current price of a LunEx stock.
Quote total should reflect our 2% commission on all LunEx transactions, plus the flat LunEx communication surcharge of $10.
Be sure we invoke LunEx once per quote, as they charge $0.05 per invocation!
Write StockQuote class
![Page 14: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/14.jpg)
April 12, 2023 © Agile Institute 2012 14
StockQuote
+ double total()
LunExServices
+ double currentPrice( String symbol )
DATABASE
![Page 15: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/15.jpg)
Testing with a Mock
April 12, 2023 © Agile Institute 2012 15
StockQuote
+ double total()
LunExServices
+ double currentPrice( String symbol )
StockQuoteTests
+ totalWorksRight()
MockLunExServices
+ double currentPrice( String symbol )
defines
arranges
![Page 16: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/16.jpg)
April 12, 2023 © Agile Institute 2012 16
Create a StockQuote for the current price of a LunEx stock.
Quote total should reflect our 2% commission on all LunEx transactions, plus the flat LunEx communication surcharge of $10.
Be sure we invoke LunEx once per quote, as they charge $0.05 per invocation!
Write StockQuote class
![Page 17: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/17.jpg)
April 12, 2023 © Agile Institute 2012 17
a test
@Testpublic void totalCalculatesCorrectly() {
// givenLunExServices myService = Mockito.mock(LunExServices.class);Mockito.when(myService.currentPrice("HE3"))
.thenReturn(12.00);
StockQuote quote = new StockQuote("HE3", 100, myService);
// whendouble total = quote.total();// thenAssert.assertEquals(1234.00, total, 0.00001);
}
![Page 18: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/18.jpg)
April 12, 2023 © Agile Institute 2012 18
just enough to compile & fail
package earthboundInvestments;
import lunEx.LunExServices;
public class StockQuote {public StockQuote(String symbol,
int numberOfShares,LunExServices service) {
}
public double total() {return 0;
}}
![Page 19: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/19.jpg)
April 12, 2023 © Agile Institute 2012 19
![Page 20: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/20.jpg)
April 12, 2023 © Agile Institute 2012 20
“Obvious Implementation”
public class StockQuote {private final double total;public StockQuote(String symbol,
int numberOfShares,LunExServices service) {
total = service.currentPrice(symbol) * numberOfShares
* 1.02 + 10.00;}
public double total() {return total;
}}
![Page 21: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/21.jpg)
April 12, 2023 © Agile Institute 2012 21
![Page 22: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/22.jpg)
April 12, 2023 © Agile Institute 2012 22
what else?@Testpublic void totalCalculatesCorrectly() {
// givenLunExServices myService = Mockito.mock(LunExServices.class);Mockito.when(myService.currentPrice("HE3"))
.thenReturn(12.00);
StockQuote quote = new StockQuote("HE3", 100, myService);
// whendouble total = quote.total();// thenAssert.assertEquals(1234.00, total, 0.00001);Mockito.verify(myService, Mockito.times(1))
.currentPrice("HE3");}
![Page 23: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/23.jpg)
April 12, 2023 © Agile Institute 2012 23
test using hand-crafted mock
@Testpublic void totalCalculatesCorrectly_usingHandCraftedMock() {
// givenMockLunExServices myService = new
MockLunExServices(12.00);
StockQuote quote = new StockQuote("HE3", 100, myService);
// whendouble total = quote.total();
// thenAssert.assertEquals(1234.00, total, 0.00001);
Assert.assertTrue(myService.wasCalledOnceAndOnlyOnce());}
![Page 24: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/24.jpg)
April 12, 2023 © Agile Institute 2012 24
hand-crafted mock
public class MockLunExServices extends LunExServices {private double valueToReturn;private int callCount = 0;
public MockLunExServices(double valueToReturn) {this.valueToReturn = valueToReturn;
}@Overridepublic double currentPrice(String symbol) {
callCount++;return valueToReturn;
}
public boolean wasCalledOnceAndOnlyOnce() {return callCount == 1;
}}
![Page 25: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/25.jpg)
April 12, 2023 © Agile Institute 2012 25
Summary:
1. Agile: Good!2. “Agilist’s Dilemma”: Very bad.3. TDD: Great!4. Slow, non-deterministic tests: Bad.5. Mocks: Good.6. “Never an Absolutist!”
![Page 26: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/26.jpg)
April 12, 2023 © Agile Institute 2012 26
![Page 27: Mock Objects from Concept to Code](https://reader036.fdocuments.in/reader036/viewer/2022081519/5564b047d8b42a366c8b56aa/html5/thumbnails/27.jpg)
April 12, 2023 © Agile Institute 2012 27
http://PowersOfTwo.agileInstitute.com/
@agilecoach