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
Mocking for Unit Test using Mockito
Search
Eko Kurniawan Khannedy
February 14, 2018
Technology
1
320
Mocking for Unit Test using Mockito
Mocking for Unit Test using Mockito
Eko Kurniawan Khannedy
February 14, 2018
Tweet
Share
More Decks by Eko Kurniawan Khannedy
See All by Eko Kurniawan Khannedy
Monolith to Event-Driven Microservices
khannedy
1
240
Refactoring
khannedy
0
310
Multi-Datacenter Kafka at Blibli.com
khannedy
2
1.5k
QA Tools - Research and Development
khannedy
0
270
Reactive Puzzle
khannedy
0
190
Event-Driven Architecture
khannedy
1
1.8k
Resilience Engineering with Hystrix and Spring
khannedy
1
550
Centralized Configuration using Consul and Spring Cloud
khannedy
2
640
Validation Best Practice
khannedy
2
1.2k
Other Decks in Technology
See All in Technology
生成AIのガバナンスの全体像と現実解
fnifni
1
190
権威ドキュメントで振り返る2024 #年忘れセキュリティ2024
hirotomotaguchi
2
740
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
180
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
220
Postman と API セキュリティ / Postman and API Security
yokawasa
0
200
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
170
Storage Browser for Amazon S3
miu_crescent
1
140
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
2
110
OpenAIの蒸留機能(Model Distillation)を使用して運用中のLLMのコストを削減する取り組み
pharma_x_tech
4
560
Featured
See All Featured
Making Projects Easy
brettharned
116
5.9k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Navigating Team Friction
lara
183
15k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
4 Signs Your Business is Dying
shpigford
181
21k
A Philosophy of Restraint
colly
203
16k
For a Future-Friendly Web
brad_frost
175
9.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Transcript
MOCKING FOR UNIT TEST USING MOCKITO
MOCKING FOR UNIT TEST USING MOCKITO EKO KURNIAWAN KHANNEDY ▸
Principal R&D Engineer at Blibli.com ▸ Part of R&D Team at Blibli.com ▸
[email protected]
MOCKING FOR UNIT TEST USING MOCKITO AGENDA ▸ Mocking ▸
Stubbing ▸ Verification ▸ Capturing
MOCKING MOCKING FOR UNIT TEST
MOCKING IS CREATING OBJECTS THAT MIMIC THE BEHAVIOR OF REAL
OBJECTS What is Mocking? MOCKING FOR UNIT TEST USING MOCKITO
None
MOCKING FOR UNIT TEST USING MOCKITO MOCKING USING MOCKITO import
static org.mockito.Mockito.*; HttpServletRequest request = mock(HttpServletRequest.class);
MOCKING FOR UNIT TEST USING MOCKITO MOCKING USING MOCKITO JUNIT
RULE @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Mock private HttpServletRequest request;
MOCKING FOR UNIT TEST USING MOCKITO INJECTING MOCK WITH MOCKITO
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); @Mock private HttpServletRequest request; @InjectMocks private MyController controller;
STUBBING MOCKING FOR UNIT TEST
ADD BEHAVIOR TO MOCKING OBJECT What is Stubbing? MOCKING FOR
UNIT TEST USING MOCKITO
MOCKING FOR UNIT TEST USING MOCKITO STUBBING USING MOCKITO when(request.getParameter("name")).thenReturn(...);
assertEquals(..., request.getParameter("name")); when(request.getParameter("error")).thenThrown(...); request.getParameter("error"); // throw exception when(request.getParameter("return")).thenAnswer(...);
VERIFICATION MOCKING FOR UNIT TEST
VERIFY INTERACTION TO MOCKING OBJECT What is Verification? MOCKING FOR
UNIT TEST USING MOCKITO
MOCKING FOR UNIT TEST USING MOCKITO STUBING USING MOCKITO verify(request).getParameter("name");
verify(request, times(2)).getParameter("..."); verifyNoMoreInteractions(request);
CAPTURING MOCKING FOR UNIT TEST
CAPTURING VALUE THAT INTERACT WITH MOCKING OBJECT What is Capturing?
MOCKING FOR UNIT TEST USING MOCKITO
MOCKING FOR UNIT TEST USING MOCKITO CAPTURING USING MOCKITO JUNIT
RULE @Captor private ArgumentCaptor<Merchant> merchantCaptor;
MOCKING FOR UNIT TEST USING MOCKITO GET VALUE FROM CAPTOR
verify(merchantRepository).save(merchantCaptor.capture()); Merchant merchant = merchantCaptor.getValue();
DEMO
MOCKING FOR UNIT TEST USING MOCKITO REFERENCES ▸ https://github.com/mockito/mockito/wiki ▸
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features- testing.html