Forum Stats

  • 3,816,600 Users
  • 2,259,212 Discussions
  • 7,893,516 Comments

Discussions

mock object is null using mockito framework

User_E80IS
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.

@Singleton
public class BookController {

private CartService cartService;

@Inject
public BookController(CartService cartService){
  this.cartService = cartService;
}

@Inject
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);

@InjectMocks
private BookController bookController;
}


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

Answers