B
B
Biaci_Anj2021-08-26 12:33:49
Java
Biaci_Anj, 2021-08-26 12:33:49

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());

Or, for example, in testing controllers, do I need to check that the object received as a result of @RequestBody is passed to the service using refeq(), or can I safely call any() again.

Testing is new to me and I look at code examples I find on the internet and often see any(). And I am in a total misunderstanding whether I should have written refeq () everywhere.

Is there a checklist of what to check?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-08-26
@xez

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 question

Ask a Question

731 491 924 answers to any question