Skip to Main Content

mock object is null using mockito framework

User_E80ISApr 3 2022

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.

Comments
Post Details
Added on Apr 3 2022
1 comment
6,253 views