Spy with Mockito - Spy vs Mock
ฝัง
- เผยแพร่เมื่อ 5 ก.ย. 2024
- ► LEARN "Big Picture" of FULL-STACK, CLOUD, AWS, MICROSERVICES with DOCKER and KUBERNETES in **30 MINUTES** - links.in28minu...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Join Our Free Courses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
► FREE 5 DAY CHALLENGE - Learn Spring and Spring Boot - links.in28minu...
► Learn Spring Boot in 10 Steps - links.in28minu...
► Learn Docker in 10 Steps - app.rebrandly....
► Download Presentation and Notes - courses.in28mi...
► All Spring Boot Articles - www.springboott...
LEARN "Big Picture" of FULL-STACK, CLOUD, AWS, MICROSERVICES with DOCKER and KUBERNETES in **30 MINUTES** - • 01 - Microservices, Do...
Follow Ranga on LinkedIn - / rangakaranam_thank-you...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Our Top 10 Courses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30+ Courses. 500,000+ Learners. Amazing Reviews.
► 1. Master DEVOPS with Docker, Kubernetes and Azure DevOps - links.in28minu...
► 2. Become FULL STACK DEVELOPER with SPRING BOOT and REACT - links.in28minu...
► 3. Master MICROSERVICES with Spring Boot and Spring Cloud - links.in28minu...
► 4. Become AWS CERTIFIED Solution Architect - links.in28minu...
► 5. Learn SPRING in 100 Steps - links.in28minu...
► 6. JAVA PROGRAMMING for Complete Beginners in 250 Steps - links.in28minu...
► 7. Go FULL STACK DEVELOPER with Spring Boot and Angular - links.in28minu...
► 8. Master Java Unit Testing with Spring Boot & Mockito - links.in28minu...
► 9. Spring INTERVIEW GUIDE - links.in28minu...
► 10. Java INTERVIEW GUIDE - links.in28minu...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FREE Courses For You
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
► 01. Learn Full Stack, AWS, Cloud, and Microservices - • Learning Paths - Java ...
► 02. Learn Spring and Spring Boot in 5 DAYS - links.in28minu...
► 03. AWS Certified Solutions Architect Associate - links.in28minu...
► 04. Getting Started with DevOps and Cloud - links.in28minu...
► 05. Learn Docker in 10 Steps - links.in28minu...
► 06. Learn Kubernetes in 10 Steps - links.in28minu...
► 07. Learn AWS in 10 Steps - links.in28minu...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Other Recommendations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
► MOST WATCHED VIDEOS - • Our Most Watched Tutor...
► 25+ PLAYLISTS - www.youtube.co...
What is a Spy? How do you spy with Mockito?
Github : github.com/in2...
in28Minutes Course Guide - courses.in28mi...
What You Will Learn during this Step:
- Understand what a Spy does?
- Creating a spy with Mockito?
- Overriding specific methods in a spy?
Code Snippets
import static org.mockito.Mockito.spy;
List listSpy = spy
I was literally suffering to understand spy by words. Thanks to you now!
A person can learn more and put more attention if you show him/ her with error cases. Better to do like that way
Beautifully explained
clear explanation!
A M A Z I N G !!!
Really Great
Good and comprehensive video
Great
great videos thank you :)
So, when we use mock, we can't do below thing, since we it doesn't creates real object?
List arrayList = mock(ArrayList.class);
arrayList.add("sss");
assertEquals(1,arrayList.size());
Yes, when we use Mock, we can't do below thing. This is because to create a stub of a specific method of a class, the return type needs to be the same as the real implementation. List.add() returns a boolean and not a new list with the added object. This makes it so that we can't return a list with "sss" when we do .add for a Mocked arraylist. For example:
//Spy:
//Given
List spyArrayList = Mockito.spy(new ArrayList());
//When
spyArrayList.add("sss");
//Then
assertEquals(1, spyArrayList.size());
// This will return true.
// For a mock, you need to DEFINE a stub
// for each method. Because the logic is gone.
// You have to specifically tell what outcome it should return for a specific input.
//Mock:
//Given
List mockArrayList = Mockito.mock(ArrayList.class);
List listWeWantNack = new ArrayList(List.of("sss"));
//Next line doesn't work, since we can't specify a return type of arraylist,
//because .add expects a boolean back.
//This means we can't return an added list of stuff, because we can't override the .add method of list.
//Mockito.when(mockArrayList.add("sss")).thenReturn(listWeWantNack);
Sidenote:
A Spy gives the same utility as the real implementation as you can see in the example, but you can specifically stub specific methods with a spy. This is quite powerful, since you don't have to specify what to return for every method of a mocked class, unlike a Mock where you have to specify what to return for EVERY method.
This is why a Spy is called "partially mocked".
its a good tutorial and clear one :)
Please add a tutorial on mocking SQLiteDatabase. Thanks in advance.
python add is so annoying ....