EDIT: Field injections are widely considered (including myself) as bad practice. Read
here for more info. I would suggest to use constructor injection instead.
Dependency injection is very powerful feature of Inversion of Control containers like Spring and EJB. It is always good idea to encapsulate injected values into private fields. But encapsulation of autowired fields
decreases testability.
I like the way how Mockito solved this problem to mock autowired fields. Will explain it on example. (This blog post expects that you are little bit familiar with Mockito syntax, but it is self-descriptive enough
though.)
Here is first dependency of testing module. It is Spring singleton bean. This class will be mocked in the test.
1
2
3
4
5
6
|
@Repository public
public
int
throw
"Fail ); } } |
Here is second dependency of testing class. It is also Spring component. This class will be spied (partially mocked) in test. Its methodcalculatePriceForOrder
will
be invoked unchanged. Second method will be stubbed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Service public
public
throw
"Fail ); } public
int
0 ; for
orderPrice } return
} } |
And here is class under test. It autowires dependencies above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Service public
@Autowired private
@Autowired private
public
int
Order return
} } |
Finally here is test example. It uses field level annotations:
@InjectMocks
– Instantiates testing object instance and tries to inject fields annotated with@Mock
or@Spy
into
private fields of testing object@Mock
– Creates mock instance of the field it annotates@Spy
– Creates spy for instance of annotated field
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public
private
15 ; private
2 ; private
1 ; @InjectMocks private
@Spy private
@Mock private
@BeforeMethod public
MockitoAnnotations.initMocks( this ); } @Test public
Order new
Mockito.when(orderDao.getOrder(TEST_ORDER_ID)).thenReturn(order); //notice Mockito.doReturn(TEST_SHIRT_PRICE).when(priceService).getActualPrice(Item.SHIRT); Mockito.doReturn(TEST_SHOES_PRICE).when(priceService).getActualPrice(Item.SHOES); //call int
Assert.assertEquals(TEST_SHIRT_PRICE } } |
So what happen when you run this test:
- First of all TestNG framework picks up
@BeforeMethod
annotation and invokesinitMocks
method - This method invokes special Mockito call (
MockitoAnnotations.initMocks(this)
) to initialize annotated fields. Without this call, these objects would benull
.
Common mistake with this approach is to forget this invocation. - When all the test fields are populated with desired values, test is called.
This example doesn’t include Spring context creation and Spring’s annotations are here only as examples for usage against production code. Test itself doesn’t include any dependency to Spring and
ignores all its annotations. In fact there could be used EJB annotations instead or it can be running against plain (non IoC managed) private fields.
Developers tend to think about MockitoAnnotations.initMocks(this)
call as unnecessary overhead. But it is actually very handy,
because it resets testing object and re-initializes mocks. You can use it for example
- When you have various test methods using same annotated instances to ensure that various test runs doesn’t use same recorded behavior
- When repetitive / parametrized tests are used. For example you can include this call into test method itself and receive spy object as test parameter (as part of test case). This ability is very sexy in conjunction to TestNG
@DataProvider
feature
(Will explain this in different blog post).
@Spy
annotated object can be created in two ways
- Automatically by Mockito framework if there is default (non-parametrized) constructor
- Or explicitly initialized (e.g. when there is only non-default constructor)
Testing object annotated by @InjectMocks
can be also initialized explicitly.