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
300
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
630
Validation Best Practice
khannedy
2
1.2k
Other Decks in Technology
See All in Technology
フロントエンド メタフレームワーク 選定の際に考えたこと
yuppeeng
0
590
元旅行会社の情シス部員が教えるおすすめなre:Inventへの行き方 / What is the most efficient way to re:Invent
naospon
2
280
いろんなものと両立する Kaggleの向き合い方
go5paopao
2
970
FOSS4G 2024 Japan コアデイ 一般発表25 PythonでPLATEAUのデータを手軽に扱ってみる
ra0kley
1
130
いざ、BSC討伐の旅
nikinusu
2
610
株式会社ログラス − エンジニア向け会社説明資料 / Loglass Comapany Deck for Engineer
loglass2019
3
28k
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
全社横断データ活用推進のコツと その負債とのつき合い方
masatoshi0205
0
170
徹底比較!HA Kubernetes ClusterにおけるControl Plane LoadBalancerの選択肢
logica0419
2
140
サイバーセキュリティと認知バイアス:対策の隙を埋める心理学的アプローチ
shumei_ito
0
190
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
7
550
音声×Copilot オンコパの世界
kasada
1
110
Featured
See All Featured
Designing for Performance
lara
604
68k
Designing the Hi-DPI Web
ddemaree
280
34k
What's in a price? How to price your products and services
michaelherold
243
12k
GraphQLとの向き合い方2022年版
quramy
43
13k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
820
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
42
2.2k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Fireside Chat
paigeccino
33
3k
For a Future-Friendly Web
brad_frost
175
9.4k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
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