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
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
2
610
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
130
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
320
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
160
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
Can We Measure Developer Productivity?
ewolff
1
150
複雑なState管理からの脱却
sansantech
PRO
1
150
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
750
ドメイン名の終活について - JPAAWG 7th -
mikit
33
20k
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
840
Featured
See All Featured
The Language of Interfaces
destraynor
154
24k
Raft: Consensus for Rubyists
vanstee
136
6.6k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Ruby is Unlike a Banana
tanoku
97
11k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Why Our Code Smells
bkeepers
PRO
334
57k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Facilitating Awesome Meetings
lara
50
6.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
It's Worth the Effort
3n
183
27k
Site-Speed That Sticks
csswizardry
0
26
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
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