Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any best practices for naming custom ArgumentMatchers


Are there any best practices for naming custom ArgumentMatchers


When naming custom `ArgumentMatchers`, it's essential to follow best practices that enhance readability and maintainability of your code. Here are some guidelines to help you name your custom matchers effectively:

1. Reflect the Matcher's Purpose**

Ensure that the name clearly reflects the purpose or condition that the matcher checks. For example, if you have a matcher that checks if a list has a specific size, you might name it `listOfSize` or `hasSize`.

java
private ArgumentMatcher> listOfSize(final int size) {
    return argument -> argument.size() == size;
}

2. Use Descriptive Names**

Use descriptive names that indicate what the matcher does. This helps other developers quickly understand the intent of the matcher without needing to read its implementation.

java
private ArgumentMatcher containsSubstring(final String substring) {
    return argument -> argument.contains(substring);
}

3. Follow Standard Naming Conventions**

Follow standard Java naming conventions. Use camelCase for method names and PascalCase for class names. If your matcher is a class, it should be named in a way that clearly indicates its purpose, such as `StringContainsMatcher`.

4. Avoid Ambiguity**

Avoid names that could be ambiguous or misleading. Ensure that the name does not suggest a different functionality than what it actually performs.

5. Consider Readability in Use Cases**

Consider how the name will look when used in your tests. The name should be clear and readable when used with methods like `argThat()`.

java
when(fileFilter.accept(argThat(hasLuck))).thenReturn(true);

In this example, `hasLuck` clearly communicates the condition being matched.

6. Document Your Matchers**

If your matchers are complex or used across multiple tests, consider documenting them with comments or Javadoc to explain their purpose and usage.

java
/**
 * Matches a file name that ends with "luck".
 */
private ArgumentMatcher hasLuck = file -> file.getName().endsWith("luck");

By following these guidelines, you can create custom `ArgumentMatchers` that are easy to understand and use, enhancing the clarity and maintainability of your tests.

Citations:
[1] https://stackoverflow.com/questions/56565518/creating-a-custom-argument-matcher-confusing-implementation
[2] https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.mockito.ArgumentMatcher
[3] https://forum.uipath.com/t/what-is-the-standard-practice-for-naming-variable-arguments-and-workflows-that-is-followed-across-industries/306050
[4] https://www.toptal.com/java/a-guide-to-everyday-mockito
[5] https://nsubstitute.github.io/help/argument-matchers/
[6] https://mediatool.com/blog/naming-conventions
[7] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.matches
[8] https://stackoverflow.com/questions/48436231/mockito-java-8-argument-matcher-anyclass-t
[9] https://www.softwaretestinghelp.com/mockito-matchers/
[10] https://github.com/rspec/rspec-mocks/issues/1179