mockito . introduction what is mockito? – mockito is mocking frame work for java objects
TRANSCRIPT
![Page 1: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/1.jpg)
Mockito
www.luckyacademy.com
![Page 2: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/2.jpg)
Introduction
• What is Mockito?– Mockito is mocking frame work for Java Objects.
![Page 3: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/3.jpg)
Introduction…
• Why do we need to Mock Objects?– When an unit of code depended upon object that
will not be available during test or development. We can create a mock object of it.
![Page 4: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/4.jpg)
Introduction..
• What is Mock Object– A mock object is a dummy implementation for an
interface or a class in which you define the output of certain method calls
![Page 5: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/5.jpg)
Mock frameworks
• Mockito– http://code.google.com/p/mockito/
• jMock– http://jmock.org/
• EasyMock– http://easymock.org/
![Page 6: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/6.jpg)
Mockito
• Mockito is a popular mock framework which can be used in conjunction with JUnit. Mockito allows you to create and configure mock objects
![Page 7: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/7.jpg)
Create a Mock Object
• Mockito supports the creation of mock objects with the static mock() method call. It also supports the creation of mock objects based on the @Mock annotation– mathObj= mock(Math.class); //Create Math
Object
![Page 8: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/8.jpg)
Configuring the mock objects
• You can use when(....).thenReturn(....) can be used to specify a condition and a return value for this condition.– when(mathObj.add(1, 2)).thenReturn(3); //
Configure it to return 3 when arguments passed are 1,2
![Page 9: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/9.jpg)
Configure Mock Objects to throw Exceptions
• when(mathObj.div(anyInt(), eq(0))).thenThrow(new ArithmeticException()); // Configure it to return exception when denominator is zero
![Page 10: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/10.jpg)
Verify
• Mockito keeps track of all the method calls and their parameters to the mock object. You can use the verify() method on the mock object to verify that the specified conditions are met, i.e., that a method has been called with certain parameters– //Verify whether add method is tested with
arguments 1,2 – verify(mathObj).add(eq(1), eq(2));
![Page 11: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/11.jpg)
Limitations
• Following constructs cannot be tested– final classes– anonymous classes– primitive types
![Page 12: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/12.jpg)
Resources
• Our Blog– http://
luckyacademy.blogspot.in/2014/09/mockito-tutorial-part1.html
• Source Code of Tutorial– http://www.luckyacademy.com/downloads.html
![Page 13: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects](https://reader030.vdocuments.mx/reader030/viewer/2022020800/56649d745503460f94a54f46/html5/thumbnails/13.jpg)
CitationI cant take any credit for this, as I have merely gathered information from below references
• http://www.vogella.com/tutorials/Mockito/article.html
• http://gojko.net/2009/10/23/mockito-in-six-easy-examples/