Mockito Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(13 intermediate revisions by the same user not shown)
Line 4: Line 4:


=Overview=
=Overview=
The Mockito framework helps with the creation and management of external dependencies, and provides an API and annotations to create [[Mockito_Concepts#Managed_Test_Double|managed test doubles]], most often [[Software_Testing_Concepts#Mock|mock objects]]. Mockito works by dynamically creating proxies that intercept invocation into the [[Mockito_Concepts#Managed_Test_Double|managed test double]] objects and reacting the way they were programmed - return pre-defined answers, throw exceptions, verify the invocation, etc. It provides a fluent API to express all these situations programmatically. 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 [[#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=
<font color=darkgray>Why do I need this? Won't the test run otherwise?</font>
<syntaxhighlight lang='java'>
@RunWith(MockitoJUnitRunner.class)
public class SomeTest {
  @Test
  public void someTestMethod() {
    ...
  }
}
</syntaxhighlight>
=<tt>Mockito.mock()</tt>=
=<tt>Mockito.mock()</tt>=
{{Internal|Mockito_Programming_Model#API|Programming Model &#124; API}}
{{Internal|Mockito_Programming_Model#API|Programming Model &#124; API}}
Line 10: Line 27:
=<tt>@Mock</tt>=
=<tt>@Mock</tt>=
{{Internal|Mockito_Programming_Model#Annotations|Programming Model &#124; Annotations}}
{{Internal|Mockito_Programming_Model#Annotations|Programming Model &#124; Annotations}}
=Can Mock=
* Non-final classes
* Interfaces
=Can't Mock=
=Can't Mock=
* Final classes
* Final classes
Line 20: Line 41:
* Primitive types
* Primitive types
[[PowerMock]] can mock these constructs.
[[PowerMock]] can mock these constructs.
=Managed Test Double=
=Managed Test Double=
A [[Software Testing Concepts#Test_Double|test double]] managed by Mockito.
A [[Software Testing Concepts#Test_Double|test double]] managed by Mockito. It can be created either via the API with <code>[[#Mockito.mock.28.29|Mockito.mock(<object>)]]</code> or by annotating the field referencing the object in question with [[#@Mock|@Mock]].
=Mockito Stub=
{{Internal|Mockito_Programming_Model#Implementing_Stub_Functionality|Mockito Programming Model &#124; Implementing Stubs}}
=Mockito Spy=
{{Internal|Mockito_Programming_Model#Implementing_Spys|Mockito Programming Model &#124; Implementing Spys}}

Latest revision as of 21:18, 2 September 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, verify the invocation, etc. It provides a fluent API to express all these situations programmatically. 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

Why do I need this? Won't the test run otherwise?

@RunWith(MockitoJUnitRunner.class)
public class SomeTest {
  @Test
  public void someTestMethod() {
    ...
  }
}

Mockito.mock()

Programming Model | API

@Mock

Programming Model | Annotations

Can Mock

  • Non-final classes
  • Interfaces

Can't Mock

  • Final classes
  • Final methods
  • Enums
  • Static methods
  • Private methods
  • hashCode() and equals() 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.

Mockito Stub

Mockito Programming Model | Implementing Stubs

Mockito Spy

Mockito Programming Model | Implementing Spys