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
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
12k
The Rise of LLMOps
asei
9
1.7k
プロダクト活用度で見えた真実 ホリゾンタルSaaSでの顧客解像度の高め方
tadaken3
0
200
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
150
Platform Engineering for Software Developers and Architects
syntasso
1
520
TypeScript、上達の瞬間
sadnessojisan
46
13k
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
アジャイルでの品質の進化 Agile in Motion vol.1/20241118 Hiroyuki Sato
shift_evolve
0
180
RubyのWebアプリケーションを50倍速くする方法 / How to Make a Ruby Web Application 50 Times Faster
hogelog
3
950
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
550
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
3
630
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Designing for Performance
lara
604
68k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
BBQ
matthewcrist
85
9.3k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Rails Girls Zürich Keynote
gr2m
94
13k
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