Total Pageviews

2014/12/09

How to mock autowired fields via Mockito

Problem
Assume we wrote a calculation service which provide add and subtract service.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

/**
 * The Class CalculationService.
 */
@Service
@Slf4j
public class CalculationService {

    public int add(int num1, int num2) {
        return num1 + num2;
    }
    
    public int subtract(int num1, int num2) {
        return num1 - num2;
    }
    
}

If we would like to do unit test for CalculationService class, and autowire CalculationService into service class as bellows:
 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
import static org.junit.Assert.assertEquals;
import gov.nta.nss.service.CalculationService;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class CalculationServiceTest {

    /* Inject CalculationService */
    @Autowired
    private CalculationService service;

    /* Test add function in CalculationService */
    @Test
    public void testAdd() {
        // arrange & act
        int result = service.add(6, 5);

        // assert
        assertEquals(11, result);
    }

    /* Test subtract function in CalculationService */
    @Test
    public void testSubtract() {
        // arrange & act
        int result = service.subtract(6, 5);

        // assert
        assertEquals(1, result);
    }

}

As I executed this test class, it returned NullPointerException. This exception result from failed to autowire CalculationService. How to overcome this problem?



Solution
We can mock autowired fields by Mockito to solve this problem. Here is the revised test code:
 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
35
36
37
38
39
40
41
import static org.junit.Assert.assertEquals;
import gov.nta.nss.service.CalculationService;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;

public class CalculationServiceTest {

    /* Inject CalculationService */
    @InjectMocks
    private CalculationService service;

    /* Initialized mocks */
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    /* Test add function in CalculationService */
    @Test
    public void testAdd() {
        // arrange & act
        int result = service.add(6, 5);

        // assert
        assertEquals(11, result);
    }

    /* Test subtract function in CalculationService */
    @Test
    public void testSubtract() {
        // arrange & act
        int result = service.subtract(6, 5);

        // assert
        assertEquals(1, result);
    }

}

Test result is bellowing:


Reference
[1] http://lkrnac.net/blog/2014/01/mock-autowired-fields/
[2] http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html


No comments: