Its my early days with Mockito so need help. Because of the new() operator, a new object is getting created and the Mock is losing significance. Goal is to prevent execution of the stockValidator.validateRequest() method. Kindly note source code can't be modified. Sample code shared below,
Is there a way to mock the new() object?
StockResponse buyStock(StockRequest stockRequest) method{
.......
StockValidator stockValidator = new StockValidator(.....);
StockError error = stockValidator.validateRequest(StockRequest stockRequest);
}
StockError validateRequest(StockRequest stockRequest){
......
......
}
@MockBean
StockValidator stockValidator;
@Autowire
StockService stockService;
............
............
when(cardValidator.validateRequest(any(), any())).thenReturn(null);
StockResponse response = stockService.buyStock(stockRequest);