Answer the question
In order to leave comments, you need to log in
Do I need to check when testing whether methods with the exact match on the desired object were called with refeq() or any() is enough?
Let's say I need to test the save method. Do I need to check that repository.save() is called on a specific object? Or any() is enough, like here?
@Test
void givenProductToAddShouldReturnAddedProduct() throws ProductAlreadyExistsException{
//stubbing
when(productRepository.save(any())).thenReturn(product1);
productService.addProduct(product1);
verify(productRepository,times(1)).save(any());
Answer the question
In order to leave comments, you need to log in
any() is used for mocks. Mocks don't check anything, mocks are conventions. Therefore, there is any().
If you want to check the saving in the database - you need to raise the database, save the object, get the saved object from the database and compare with the original one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question