Use Mockito to mock autowired fields

twittergoogle_plusrss

Facebooktwittergoogle_plusredditlinkedinmail

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.

@Repository
public class OrderDao {
	public Order getOrder(int irderId){
		throw new UnsupportedOperationException("Fail is not mocked!");
	}
}

Here is second dependency of testing class. It is also Spring component. This class will be spied (partially mocked) in test. Its method calculatePriceForOrder will be invoked unchanged. Second method will be stubbed.

@Service
public class PriceService {
	public int getActualPrice(Item item){
		throw new UnsupportedOperationException("Fail is not mocked!");
	}

	public int calculatePriceForOrder(Order order){
		int orderPrice = 0;
		for (Item item : order.getItems()){
			orderPrice += getActualPrice(item);
		}
		return orderPrice;
	}
}

And here is class under test. It  autowires dependencies above.

@Service
public class OrderService {

	@Autowired
	private PriceService priceService;

	@Autowired
	private OrderDao orderDao;

	public int getOrderPrice(int orderId){
		Order order = orderDao.getOrder(orderId);
		return priceService.calculatePriceForOrder(order);
	}
}

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
public class OrderServiceTest {
	private static final int TEST_ORDER_ID = 15;
	private static final int TEST_SHOES_PRICE = 2;   
	private static final int TEST_SHIRT_PRICE = 1;

	@InjectMocks
	private OrderService testingObject;

	@Spy
	private PriceService priceService;

	@Mock
	private OrderDao orderDao;

	@BeforeMethod
	public void initMocks(){
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void testGetOrderService(){
		Order order = new Order(Arrays.asList(Item.SHOES, Item.SHIRT));
		Mockito.when(orderDao.getOrder(TEST_ORDER_ID)).thenReturn(order);

		//notice different Mockito syntax for spy
		Mockito.doReturn(TEST_SHIRT_PRICE).when(priceService).getActualPrice(Item.SHIRT);
		Mockito.doReturn(TEST_SHOES_PRICE).when(priceService).getActualPrice(Item.SHOES);

		//call testing method
		int actualOrderPrice = testingObject.getOrderPrice(TEST_ORDER_ID);

		Assert.assertEquals(TEST_SHIRT_PRICE + TEST_SHOES_PRICE, actualOrderPrice);
	}
}

So what happen when you run this test:

  1. First of all TestNG framework picks up @BeforeMethod annotation and invokes initMocks method
  2. This method invokes special Mockito call (MockitoAnnotations.initMocks(this)) to initialize annotated fields. Without this call, these objects would be null. Common mistake with this approach is to forget this invocation.
  3. 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.

Example source code can be downloaded from GitHub.

twittergoogle_plusrss

6 thoughts on “Use Mockito to mock autowired fields

  1. I tried this project and got NPE when reaching this line

    Mockito.when(orderDao.getOrder(TEST_ORDER_ID)).thenReturn(order);

    Seems both orderDao and testingObject are null.

    1. Works fine here.
      I cloned into new directory from Github, and run ‘mvn test’:

      [INFO] Scanning for projects…
      [INFO]
      [INFO] ————————————————————————
      [INFO] Building mock-autowired 0.0.1-SNAPSHOT
      [INFO] ————————————————————————
      [INFO]
      [INFO] — maven-resources-plugin:2.5:resources (default-resources) @ mock-autowired —
      [debug] execute contextualize
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] skip non existing resourceDirectory /home/sitko/prg/projects/blogs/test/blog-2014-01-21-mock-autowired-fields/src/main/resources
      [INFO]
      [INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ mock-autowired —
      [INFO] Changes detected – recompiling the module!
      [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
      [INFO] Compiling 5 source files to /home/sitko/prg/projects/blogs/test/blog-2014-01-21-mock-autowired-fields/target/classes
      [INFO]
      [INFO] — maven-resources-plugin:2.5:testResources (default-testResources) @ mock-autowired —
      [debug] execute contextualize
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] skip non existing resourceDirectory /home/sitko/prg/projects/blogs/test/blog-2014-01-21-mock-autowired-fields/src/test/resources
      [INFO]
      [INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mock-autowired —
      [INFO] Changes detected – recompiling the module!
      [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
      [INFO] Compiling 1 source file to /home/sitko/prg/projects/blogs/test/blog-2014-01-21-mock-autowired-fields/target/test-classes
      [INFO]
      [INFO] — maven-surefire-plugin:2.10:test (default-test) @ mock-autowired —
      [INFO] Surefire report directory: /home/sitko/prg/projects/blogs/test/blog-2014-01-21-mock-autowired-fields/target/surefire-reports
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng/2.10/surefire-testng-2.10.pom
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng/2.10/surefire-testng-2.10.pom (4 KB at 13.4 KB/sec)
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.10/surefire-providers-2.10.pom
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.10/surefire-providers-2.10.pom (3 KB at 40.2 KB/sec)
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.pom
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.pom (0 B at 0.0 KB/sec)
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom (0 B at 0.0 KB/sec)
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng-utils/2.10/surefire-testng-utils-2.10.pom
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng-utils/2.10/surefire-testng-utils-2.10.pom (3 KB at 30.8 KB/sec)
      Downloading: http://repo.maven.apache.org/maven2/org/testng/testng/5.7/testng-5.7.pom
      Downloaded: http://repo.maven.apache.org/maven2/org/testng/testng/5.7/testng-5.7.pom (12 KB at 156.2 KB/sec)
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng/2.10/surefire-testng-2.10.jar
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.jar
      Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng-utils/2.10/surefire-testng-utils-2.10.jar
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng/2.10/surefire-testng-2.10.jar (32 KB at 373.8 KB/sec)
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-testng-utils/2.10/surefire-testng-utils-2.10.jar (19 KB at 158.2 KB/sec)
      Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.jar (77 KB at 499.4 KB/sec)

      ——————————————————-
      T E S T S
      ——————————————————-
      Running net.lkrnac.testingexamples.mockautowired.OrderServiceTest
      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.109 sec

      Results :

      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

      [INFO] ————————————————————————
      [INFO] BUILD SUCCESS
      [INFO] ————————————————————————
      [INFO] Total time: 6.461s
      [INFO] Finished at: Thu Jul 14 20:43:53 CEST 2016
      [INFO] Final Memory: 17M/167M
      [INFO] ————————————————————————

      Process finished with exit code 0

Leave a Reply

Your email address will not be published. Required fields are marked *