Spy with Mockito - Spy vs Mock

  Рет қаралды 45,395

in28minutes

in28minutes

Күн бұрын

Пікірлер: 16
@rohitrox8
@rohitrox8 4 жыл бұрын
I was literally suffering to understand spy by words. Thanks to you now!
@kekiir
@kekiir Жыл бұрын
clear explanation!
@AhmadShahwaiz
@AhmadShahwaiz 4 жыл бұрын
Beautifully explained
@RamapatiMaurya
@RamapatiMaurya 2 жыл бұрын
A person can learn more and put more attention if you show him/ her with error cases. Better to do like that way
@umeshkumarroy5804
@umeshkumarroy5804 3 жыл бұрын
Really Great
@shankars4281
@shankars4281 2 жыл бұрын
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());
@thegamerz5034
@thegamerz5034 2 жыл бұрын
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".
@MaggicBones
@MaggicBones 3 жыл бұрын
A M A Z I N G !!!
@chaitanyabag7348
@chaitanyabag7348 7 жыл бұрын
Good and comprehensive video
@prasannakumarattuluri7650
@prasannakumarattuluri7650 6 жыл бұрын
its a good tutorial and clear one :)
@SanjeevSharma123
@SanjeevSharma123 8 жыл бұрын
great videos thank you :)
@maheshbeesetty5979
@maheshbeesetty5979 5 жыл бұрын
Great
@preethikashenoy
@preethikashenoy 7 жыл бұрын
Please add a tutorial on mocking SQLiteDatabase. Thanks in advance.
@ShahidKhan-qx5fh
@ShahidKhan-qx5fh 7 жыл бұрын
python add is so annoying ....
Mockito Annotations -  @Mock, @Runwith, @InjectMocks and @Captor
12:21
Mockito Tutorial - Mocking With Junit and Maven
1:11:53
in28minutes
Рет қаралды 552 М.
Car Bubble vs Lamborghini
00:33
Stokes Twins
Рет қаралды 40 МЛН
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 2,2 МЛН
When mom gets home, but you're in rollerblades.
00:40
Daniel LaBelle
Рет қаралды 148 МЛН
Mockito and BDD :  A Quick Tutorial
15:49
in28minutes
Рет қаралды 22 М.
Mocks, Stubs and Spies with Mockito - JUnit Tutorial
14:59
Aneesh Mistry
Рет қаралды 42 М.
Unit Tests and Test Doubles like Mocks, Stubs & Fakes
17:32
Cognitive Programmer
Рет қаралды 136 М.
@Mock | @InjectMocks  | Difference
5:27
Java Shastra
Рет қаралды 16 М.
Hamcrest Matchers with Mockito Tutorial
14:02
in28minutes
Рет қаралды 14 М.
JUnit Quick Start
34:12
in28minutes
Рет қаралды 48 М.
Car Bubble vs Lamborghini
00:33
Stokes Twins
Рет қаралды 40 МЛН