Forum Stats

  • 3,827,919 Users
  • 2,260,839 Discussions


mock object is null using mockito framework

User_E80IS Member Posts: 1 Green Ribbon

Writing one java app for learning. I am using Google Guice for object injection.

I have a bookController object. It depends on CartService & InventoryService. I am not able to write Junit test case for this.

public class BookController {

private CartService cartService;

public BookController(CartService cartService){
  this.cartService = cartService;

private InventoryService inventoryService;

Here is my junit code. I am using mockito to inject depended objects. But inventoryService is always coming as null. I am getting mocked cartService object correctly.

public TestBookController {

private CartService cartService = mock(CartService.class);
private InventoryService inventoryService = mock(inventoryService.class);

private BookController bookController;

Can someone plz help. I am not getting what I missed in my test class.