Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Mockito 2022-01-25
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
sullis
January 25, 2022
Programming
0
200
Mockito 2022-01-25
Unit testing with Mockito
Portland Java User Group
January 25, 2022
#java
#mockito
sullis
January 25, 2022
Tweet
Share
More Decks by sullis
See All by sullis
Dependency Management for Java - Seattle 2025-11-18
sullis
0
35
Dependency Management for Java - Portland - 2025-11-04
sullis
0
21
Dependency management for Java applications 2025-09-11
sullis
0
30
S3 NYC Iceberg meetup 2025-07-10
sullis
0
52
Amazon S3 Chicago 2025-06-04
sullis
0
130
Amazon S3 Boston 2025-05-07
sullis
0
97
Netty ConFoo Montreal 2025-02-27
sullis
0
140
GitHub Actions ConFoo Montreal 2025-02-26
sullis
0
98
Netty Portland Java User Group 2025-02-18
sullis
0
33
Other Decks in Programming
See All in Programming
AIに任せる範囲を安全に広げるためにやっていること
fukucheee
0
130
技術検証結果の整理と解析をAIに任せよう!
keisukeikeda
0
120
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
1
140
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
230
Go 1.26でのsliceのメモリアロケーション最適化 / Go 1.26 リリースパーティ #go126party
mazrean
1
390
Unity6.3 AudioUpdate
cova8bitdots
0
130
Codex の「自走力」を高める
yorifuji
0
1.2k
New in Go 1.26 Implementing go fix in product development
sunecosuri
0
430
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
13
8k
どんと来い、データベース信頼性エンジニアリング / Introduction to DBRE
nnaka2992
1
280
株式会社 Sun terras カンパニーデック
sunterras
0
2.1k
Claude Code の Skill で複雑な既存仕様をすっきり整理しよう
yuichirokato
1
370
Featured
See All Featured
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
53k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Git: the NoSQL Database
bkeepers
PRO
432
66k
Fireside Chat
paigeccino
42
3.8k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.1k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.5k
Test your architecture with Archunit
thirion
1
2.2k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
180
How to train your dragon (web standard)
notwaldorf
97
6.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
It's Worth the Effort
3n
188
29k
Everyday Curiosity
cassininazir
0
160
Transcript
Unit testing with Mockito Sean Sullivan Portland Java User Group
January 25, 2022
Unit Tests
• Mockito library • Upgrading to Mockito 4 • Extending
Mockito • Anti-patterns
Mockito is a framework for creating Mock objects
a Mock object mimics the behavior of a real object
Gilt.com checkout system
We wrote unit tests to verify: • happy path •
edge case scenarios • error handling logic
• inventory reservation • payment system • shipping cost calculator
• shipping restrictions • purchase limit service • tax calculator • discount calculator Gilt.com Checkout was dependent upon:
The test suite for Gilt.com Checkout used Mockito
We setup mock objects for: • Payment authorization • Inventory
reservation • Discount redemption • Tax calculation • Shipping calculation
Maven pom.xml <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>4.3.1</version> <scope>test</scope> </dependency>
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.Mockito.verify; import
static org.mockito.ArgumentMatcher.any;
TaxEngine taxEngine = mock(TaxEngine.class); ShippingCostCalculator shippingCalc = mock(ShippingCostCalculator.class); when(taxEngine.calculate(any())).thenReturn(new Money(5,
USD)); when(shippingCalc.calculate()).thenReturn(shippingInfo);
OrderManager unit test OrderManager manager = new OrderManager( taxEngine, shippingCalc);
manager.sync(order); manager.submit(order); verify(taxEngine, once()).calculate(any()); assertTrue(order.isSubmitted());
Mockito.when
thenReturn
How do I mimic a failure?
use thenThrow
TaxEngine taxEngine = mock(TaxEngine.class); when(taxEngine.calculate(any(), any())) .thenThrow(new TaxViolationException());
Four major versions of Mockito • version 1.x • version
2.x • version 3.x • version 4.x
Upgrading to Mockito 4.x
None
None
None
None
async-http-client PR 1812
async-http-client PR 1812
async-http-client PR 1812
async-http-client PR 1812
async-http-client PR 1812
What if my company has 500 Git repos?
Automated refactoring tool
OpenRewrite https://moderne.io/
OpenRewrite
OpenRewrite
mockito.yml
Extending Mockito
Answer interface public interface Answer<T> { T answerInvocation(InvocationOnMock invocation); }
MockitoPlus library
MockitoPlus
MockitoPlus import static org.mockito.Mockito.mock; import static io.github.mockitoplus.MockitoPlus.when;
MockitoPlus HelloWorld hello = mock(HelloWorld.class); when(hello.sayHello(any())) .thenReturn("bonjour") .failAlternatingInvocations();
MockitoPlus HelloWorld hello = mock(HelloWorld.class); when(hello.sayHello(any())) .thenReturn("bonjour") . fi rstInvocationFails();
MockitoPlus HelloWorld hello = mock(HelloWorld.class); when(hello.sayHello(any())) .thenReturn("bonjour") .intermittentFailures() .randomDelay(Duration.of(500, MILLIS));
Intermittent failures with random delays
Mockito anti-patterns
// anti-pattern List<String> names = mock(List.class); // better List<String> names
= new ArrayList<String>(); anti-pattern
// anti-pattern Optional<String> name = mock(Optional.class); // better Optional<String> name
= Optional.of(“Obama”); anti-pattern
// anti-pattern CompletableFuture<String> f = mock(CompletableFuture.class); // better CompletableFuture<String> f
= CompletableFuture.completedFuture(“bonjour”); anti-pattern
what about Scala?
None
“org.mockito” %% "mockito-scala" % “1.16.34” % Test build.sbt
import org.mockito.scalatest.MockitoSugar Scala trait
Questions?
The End @tinyrobots