For writing unit tests in JUnit, Mockito provides argument matchers that you can use to mock the arguments in a given() or when() statement. Among the matchers provided, the any() matcher lets you pass any argument to the method, ensuring that the test cases passes with any input.

When any() matcher is used extensively, out of laziness of specifying the parameters, there are high chances that the test case is not really testing anything or it passes even with a wrong input.

Consider an example of a method that contains a repository call findByUserIdAndArticleId(). Here the first parameter is supposed to be userId and second parameter articleId. Lets say while writing this method, the params were interchanged by mistake and articleId was given as the first input.

void findArticle(UUID userId, UUID articleId){
	Publication publication = publicationRepository.findByUserIdAndArticleId(articleId, userId);  //params interchanged
	...
	...
}

Such an accidental error should ideally be caught in a unit test case. While testing this method, if matchers are used to mock the userId and articleId param, the test case will end up passing even for wrong inputs since we used the any() matcher.

given(publicationRepository.findByUserIdAndArticleId(any(), any())).willReturn(publication);

So this unit test is not really testing anything. Such an issue wont happen if an actual value for userId and articleId is passed instead of mocking the values with any() matcher.

Any() matcher should be used only when the test case is not particularly concerned about the inputs for a given function call, or values for the parameters are not known beforehand. Or it can be used when the params doesnt really matter, as with a verify statement with never(), to verify that a function is never called with any params.

verify(publicationRepository, never()).findById(any());