Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

All You Need Is Lambdas: Java Tests Without a Mocking Framework

DEV Community·Tiberiu Tofan·25 days ago
#y7Fu8Sfr
Reading 0:00
15s threshold

Most Java developers have written something like this. Maybe recently. @Test void should_categorize_new_transactions () { final var mockRepository = Mockito . mock ( CategorizedTransactionRepository . class ); final var mockMerchantDirectoryService = Mockito . mock ( MerchantDirectoryService . class ); final var categorizer = new TransactionCategorizer ( mockRepository , mockMerchantDirectoryService ); final var message = createSampleTransactionMessage (); Mockito . when ( mockMerchantDirectoryService . getCategoryForMerchant ( message . mcc ())) . thenReturn ( new MerchantInfo ( message . mcc (), "Transportation" )); Mockito . when ( mockRepository . findByTransactionId ( message . transactionId ())) . thenReturn ( Optional . empty ()); Mockito . when ( mockRepository . save ( Mockito . any ())) . thenAnswer ( invocation -> { var ct = ( CategorizedTransaction ) invocation . getArgument ( 0 ); ct . setId ( 1L ); return ct ; }); final var result = categorizer . categorize ( message ); Assertions .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More