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
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
730
終了の危機にあった15年続くWebサービスを全力で存続させる - phpcon2024
yositosi
27
22k
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
160
コンテナセキュリティのためのLandlock入門
nullpo_head
2
330
[トレノケ雲の会 mod.13] 3回目のre:Inventで気づいたこと -CloudOperationsを添えて-
shintaro_fukatsu
0
110
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
3
2.6k
オプトインカメラ:UWB測位を応用したオプトイン型のカメラ計測
matthewlujp
0
190
Google Cloud で始める Cloud Run 〜AWSとの比較と実例デモで解説〜
risatube
PRO
0
120
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
290
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1.1k
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
130
株式会社ログラス − エンジニア向け会社説明資料 / Loglass Comapany Deck for Engineer
loglass2019
3
32k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
910
How STYLIGHT went responsive
nonsquared
96
5.2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
450
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
A Modern Web Designer's Workflow
chriscoyier
693
190k
We Have a Design System, Now What?
morganepeng
51
7.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Embracing the Ebb and Flow
colly
84
4.5k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
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