Mockito Concepts: Difference between revisions
Line 7: | Line 7: | ||
=Implementation= | =Implementation= | ||
Mockito creates dynamic proxies based on the [[#Managed_Test_Double|test double]] concrete class. | Mockito creates dynamic proxies based on the [[#Managed_Test_Double|test double]] concrete class. Mockito has a <code>MethodInterceptor</code> class that implements <code>java.lang.reflect.InvocationHandler</code> and intercepts any method call on the managed mock object. | ||
=MockitoJUnitRunner= | =MockitoJUnitRunner= |
Revision as of 23:29, 20 July 2021
Internal
Overview
The Mockito framework helps with the creation and management of external dependencies, and provides an API and annotations to create managed test doubles, most often mock objects. Mockito works by dynamically creating proxies that intercept invocation into the managed test double objects and reacting the way they were programmed - return pre-defined answers, throw exceptions, verifying the invocation, etc. when the invocations are sent into the managed test doubles. Mockito uses internally CGLib to create proxy stubs. CGLib is used to generate dynamic poxy objects and intercept field access.
Implementation
Mockito creates dynamic proxies based on the test double concrete class. Mockito has a MethodInterceptor
class that implements java.lang.reflect.InvocationHandler
and intercepts any method call on the managed mock object.
MockitoJUnitRunner
Mockito.mock()
@Mock
Can't Mock
- Final classes
- Final methods
- Enums
- Static methods
- Private methods
hashCode()
andequals()
methods- Anonymous classes
- Primitive types
PowerMock can mock these constructs.
Managed Test Double
A test double managed by Mockito. It can be created either via the API with Mockito.mock(<object>)
or by annotating the field referencing the object in question with @Mock.