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".
I was literally suffering to understand spy by words. Thanks to you now!
clear explanation!
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
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".
Really Great
A M A Z I N G !!!
Good and comprehensive video
its a good tutorial and clear one :)
great videos thank you :)
Great
Please add a tutorial on mocking SQLiteDatabase. Thanks in advance.
python add is so annoying ....