mockito . introduction what is mockito? – mockito is mocking frame work for java objects

13
Mockito www.luckyacademy.com

Upload: toby-phillips

Post on 22-Dec-2015

225 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects

Mockito

www.luckyacademy.com

Page 2: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects

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

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

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

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

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

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

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

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

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

Limitations

• Following constructs cannot be tested– final classes– anonymous classes– primitive types

Page 13: Mockito . Introduction What is Mockito? – Mockito is mocking frame work for Java Objects

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/