Best Mockito code snippet using org.mockito.ArgumentMatchers.reportMatcher
Source:ArgumentMatchers.java
...133 * @see #anyInt()134 * @see #anyBoolean()135 */136 public static <T> T any() {137 reportMatcher(Any.ANY);138 return null;139 }140 /**141 * Matches any object of given type, excluding nulls.142 *143 * <p>144 * This matcher will perform a type check with the given type, thus excluding values.145 * See examples in javadoc for {@link ArgumentMatchers} class.146 *147 * This is an alias of: {@link #isA(Class)}}148 * </p>149 *150 * <p>151 * Since Mockito 2.1.0, only allow non-null instance of <code></code>, thus <code>null</code> is not anymore a valid value.152 * As reference are nullable, the suggested API to <strong>match</strong> <code>null</code>153 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito154 * 1.x.155 * </p>156 *157 * <p><strong>Notes : </strong><br/>158 * <ul>159 * <li>For primitive types use {@link #anyChar()} family.</li>160 * <li>Since Mockito 2.1.0 this method will perform a type check thus <code>null</code> values are not authorized.</li>161 * <li>Since mockito 2.1.0 {@link #any()} is no longer an alias of this method.</li>162 * </ul>163 * </p>164 *165 * @param <T> The accepted type166 * @param type the class of the accepted type.167 * @return <code>null</code>.168 * @see #any()169 * @see #isA(Class)170 * @see #notNull()171 * @see #isNull()172 */173 public static <T> T any(Class<T> type) {174 reportMatcher(new InstanceOf.VarArgAware(type, "<any " + type.getCanonicalName() + ">"));175 return defaultValue(type);176 }177 /**178 * <code>Object</code> argument that implements the given class.179 * <p>180 * See examples in javadoc for {@link ArgumentMatchers} class181 *182 * @param <T> the accepted type.183 * @param type the class of the accepted type.184 * @return <code>null</code>.185 * @see #any(Class)186 */187 public static <T> T isA(Class<T> type) {188 reportMatcher(new InstanceOf(type));189 return defaultValue(type);190 }191 /**192 * Any <code>boolean</code> or <strong>non-null</strong> <code>Boolean</code>193 *194 * <p>195 * Since Mockito 2.1.0, only allow valued <code>Boolean</code>, thus <code>null</code> is not anymore a valid value.196 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper197 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito198 * 1.x.199 * </p>200 *201 * <p>202 * See examples in javadoc for {@link ArgumentMatchers} class.203 * </p>204 *205 * @return <code>false</code>.206 * @see #isNull()207 */208 public static boolean anyBoolean() {209 reportMatcher(new InstanceOf(Boolean.class, "<any boolean>"));210 return false;211 }212 /**213 * Any <code>byte</code> or <strong>non-null</strong> <code>Byte</code>.214 *215 * <p>216 * Since Mockito 2.1.0, only allow valued <code>Byte</code>, thus <code>null</code> is not anymore a valid value.217 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper218 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito219 * 1.x.220 * </p>221 *222 * <p>223 * See examples in javadoc for {@link ArgumentMatchers} class.224 * </p>225 *226 * @return <code>0</code>.227 * @see #isNull()228 */229 public static byte anyByte() {230 reportMatcher(new InstanceOf(Byte.class, "<any byte>"));231 return 0;232 }233 /**234 * Any <code>char</code> or <strong>non-null</strong> <code>Character</code>.235 *236 * <p>237 * Since Mockito 2.1.0, only allow valued <code>Character</code>, thus <code>null</code> is not anymore a valid value.238 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper239 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito240 * 1.x.241 * </p>242 *243 * <p>244 * See examples in javadoc for {@link ArgumentMatchers} class.245 * </p>246 *247 * @return <code>0</code>.248 * @see #isNull()249 */250 public static char anyChar() {251 reportMatcher(new InstanceOf(Character.class, "<any char>"));252 return 0;253 }254 /**255 * Any int or <strong>non-null</strong> <code>Integer</code>.256 *257 * <p>258 * Since Mockito 2.1.0, only allow valued <code>Integer</code>, thus <code>null</code> is not anymore a valid value.259 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper260 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito261 * 1.x.262 * </p>263 *264 * <p>265 * See examples in javadoc for {@link ArgumentMatchers} class.266 * </p>267 *268 * @return <code>0</code>.269 * @see #isNull()270 */271 public static int anyInt() {272 reportMatcher(new InstanceOf(Integer.class, "<any integer>"));273 return 0;274 }275 /**276 * Any <code>long</code> or <strong>non-null</strong> <code>Long</code>.277 *278 * <p>279 * Since Mockito 2.1.0, only allow valued <code>Long</code>, thus <code>null</code> is not anymore a valid value.280 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper281 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito282 * 1.x.283 * </p>284 *285 * <p>286 * See examples in javadoc for {@link ArgumentMatchers} class.287 * </p>288 *289 * @return <code>0</code>.290 * @see #isNull()291 */292 public static long anyLong() {293 reportMatcher(new InstanceOf(Long.class, "<any long>"));294 return 0;295 }296 /**297 * Any <code>float</code> or <strong>non-null</strong> <code>Float</code>.298 *299 * <p>300 * Since Mockito 2.1.0, only allow valued <code>Float</code>, thus <code>null</code> is not anymore a valid value.301 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper302 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito303 * 1.x.304 * </p>305 *306 * <p>307 * See examples in javadoc for {@link ArgumentMatchers} class.308 * </p>309 *310 * @return <code>0</code>.311 * @see #isNull()312 */313 public static float anyFloat() {314 reportMatcher(new InstanceOf(Float.class, "<any float>"));315 return 0;316 }317 /**318 * Any <code>double</code> or <strong>non-null</strong> <code>Double</code>.319 *320 * <p>321 * Since Mockito 2.1.0, only allow valued <code>Double</code>, thus <code>null</code> is not anymore a valid value.322 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper323 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito324 * 1.x.325 * </p>326 *327 * <p>328 * See examples in javadoc for {@link ArgumentMatchers} class.329 * </p>330 *331 * @return <code>0</code>.332 * @see #isNull()333 */334 public static double anyDouble() {335 reportMatcher(new InstanceOf(Double.class, "<any double>"));336 return 0;337 }338 /**339 * Any <code>short</code> or <strong>non-null</strong> <code>Short</code>.340 *341 * <p>342 * Since Mockito 2.1.0, only allow valued <code>Short</code>, thus <code>null</code> is not anymore a valid value.343 * As primitive wrappers are nullable, the suggested API to <strong>match</strong> <code>null</code> wrapper344 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito345 * 1.x.346 * </p>347 *348 * <p>349 * See examples in javadoc for {@link ArgumentMatchers} class.350 * </p>351 *352 * @return <code>0</code>.353 * @see #isNull()354 */355 public static short anyShort() {356 reportMatcher(new InstanceOf(Short.class, "<any short>"));357 return 0;358 }359 /**360 * Any <strong>non-null</strong> <code>String</code>361 *362 * <p>363 * Since Mockito 2.1.0, only allow non-null <code>String</code>.364 * As this is a nullable reference, the suggested API to <strong>match</strong> <code>null</code> wrapper365 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito366 * 1.x.367 * </p>368 *369 * <p>370 * See examples in javadoc for {@link ArgumentMatchers} class.371 * </p>372 *373 * @return empty String ("")374 * @see #isNull()375 */376 public static String anyString() {377 reportMatcher(new InstanceOf(String.class, "<any string>"));378 return "";379 }380 /**381 * Any <strong>non-null</strong> <code>List</code>.382 *383 * <p>384 * Since Mockito 2.1.0, only allow non-null <code>List</code>.385 * As this is a nullable reference, the suggested API to <strong>match</strong> <code>null</code> wrapper386 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito387 * 1.x.388 * </p>389 *390 * <p>391 * See examples in javadoc for {@link ArgumentMatchers} class.392 * </p>393 *394 * @return empty List.395 * @see #isNull()396 */397 public static <T> List<T> anyList() {398 reportMatcher(new InstanceOf(List.class, "<any List>"));399 return new ArrayList<T>(0);400 }401 /**402 * Any <strong>non-null</strong> <code>Set</code>.403 *404 * <p>405 * Since Mockito 2.1.0, only allow non-null <code>Set</code>.406 * As this is a nullable reference, the suggested API to <strong>match</strong> <code>null</code> wrapper407 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito408 * 1.x.409 * </p>410 *411 * <p>412 * See examples in javadoc for {@link ArgumentMatchers} class.413 * </p>414 *415 * @return empty Set416 * @see #isNull()417 */418 public static <T> Set<T> anySet() {419 reportMatcher(new InstanceOf(Set.class, "<any set>"));420 return new HashSet<T>(0);421 }422 /**423 * Any <strong>non-null</strong> <code>Map</code>.424 *425 * <p>426 * Since Mockito 2.1.0, only allow non-null <code>Map</code>.427 * As this is a nullable reference, the suggested API to <strong>match</strong> <code>null</code> wrapper428 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito429 * 1.x.430 * </p>431 *432 * <p>433 * See examples in javadoc for {@link ArgumentMatchers} class.434 * </p>435 *436 * @return empty Map.437 * @see #isNull()438 */439 public static <K, V> Map<K, V> anyMap() {440 reportMatcher(new InstanceOf(Map.class, "<any map>"));441 return new HashMap<K, V>(0);442 }443 /**444 * Any <strong>non-null</strong> <code>Collection</code>.445 *446 * <p>447 * Since Mockito 2.1.0, only allow non-null <code>Collection</code>.448 * As this is a nullable reference, the suggested API to <strong>match</strong> <code>null</code>449 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito450 * 1.x.451 * </p>452 *453 * <p>454 * See examples in javadoc for {@link ArgumentMatchers} class.455 * </p>456 *457 * @return empty Collection.458 * @see #isNull()459 */460 public static <T> Collection<T> anyCollection() {461 reportMatcher(new InstanceOf(Collection.class, "<any collection>"));462 return new ArrayList<T>(0);463 }464 /**465 * Any <strong>non-null</strong> <code>Iterable</code>.466 *467 * <p>468 * Since Mockito 2.1.0, only allow non-null <code>Iterable</code>.469 * As this is a nullable reference, the suggested API to <strong>match</strong> <code>null</code>470 * would be {@link #isNull()}. We felt this change would make test harnesses much safer than they were with Mockito471 * 1.x.472 * </p>473 *474 * <p>475 * See examples in javadoc for {@link ArgumentMatchers} class.476 * </p>477 *478 * @return empty Iterable.479 * @see #isNull()480 * @since 2.1.0481 */482 public static <T> Iterable<T> anyIterable() {483 reportMatcher(new InstanceOf(Iterable.class, "<any iterable>"));484 return new ArrayList<T>(0);485 }486 /**487 * <code>boolean</code> argument that is equal to the given value.488 *489 * <p>490 * See examples in javadoc for {@link ArgumentMatchers} class491 * </p>492 *493 * @param value the given value.494 * @return <code>0</code>.495 */496 public static boolean eq(boolean value) {497 reportMatcher(new Equals(value));498 return false;499 }500 /**501 * <code>byte</code> argument that is equal to the given value.502 *503 * <p>504 * See examples in javadoc for {@link ArgumentMatchers} class505 * </p>506 *507 * @param value the given value.508 * @return <code>0</code>.509 */510 public static byte eq(byte value) {511 reportMatcher(new Equals(value));512 return 0;513 }514 /**515 * <code>char</code> argument that is equal to the given value.516 *517 * <p>518 * See examples in javadoc for {@link ArgumentMatchers} class519 * </p>520 *521 * @param value the given value.522 * @return <code>0</code>.523 */524 public static char eq(char value) {525 reportMatcher(new Equals(value));526 return 0;527 }528 /**529 * <code>double</code> argument that is equal to the given value.530 *531 * <p>532 * See examples in javadoc for {@link ArgumentMatchers} class533 * </p>534 *535 * @param value the given value.536 * @return <code>0</code>.537 */538 public static double eq(double value) {539 reportMatcher(new Equals(value));540 return 0;541 }542 /**543 * <code>float</code> argument that is equal to the given value.544 *545 * <p>546 * See examples in javadoc for {@link ArgumentMatchers} class547 * </p>548 *549 * @param value the given value.550 * @return <code>0</code>.551 */552 public static float eq(float value) {553 reportMatcher(new Equals(value));554 return 0;555 }556 /**557 * <code>int</code> argument that is equal to the given value.558 *559 * <p>560 * See examples in javadoc for {@link ArgumentMatchers} class561 * </p>562 *563 * @param value the given value.564 * @return <code>0</code>.565 */566 public static int eq(int value) {567 reportMatcher(new Equals(value));568 return 0;569 }570 /**571 * <code>long</code> argument that is equal to the given value.572 *573 * <p>574 * See examples in javadoc for {@link ArgumentMatchers} class575 * </p>576 *577 * @param value the given value.578 * @return <code>0</code>.579 */580 public static long eq(long value) {581 reportMatcher(new Equals(value));582 return 0;583 }584 /**585 * <code>short</code> argument that is equal to the given value.586 * <p>587 * See examples in javadoc for {@link ArgumentMatchers} class588 *589 * @param value the given value.590 * @return <code>0</code>.591 */592 public static short eq(short value) {593 reportMatcher(new Equals(value));594 return 0;595 }596 /**597 * Object argument that is equal to the given value.598 *599 * <p>600 * See examples in javadoc for {@link ArgumentMatchers} class601 * </p>602 *603 * @param value the given value.604 * @return <code>null</code>.605 */606 public static <T> T eq(T value) {607 reportMatcher(new Equals(value));608 if (value == null) return null;609 return (T) Primitives.defaultValue(value.getClass());610 }611 /**612 * Object argument that is reflection-equal to the given value with support for excluding613 * selected fields from a class.614 *615 * <p>616 * This matcher can be used when equals() is not implemented on compared objects.617 * Matcher uses java reflection API to compare fields of wanted and actual object.618 * </p>619 *620 * <p>621 * Works similarly to <code>EqualsBuilder.reflectionEquals(this, other, excludeFields)</code> from622 * apache commons library.623 * <p>624 * <b>Warning</b> The equality check is shallow!625 * </p>626 *627 * <p>628 * See examples in javadoc for {@link ArgumentMatchers} class629 * </p>630 *631 * @param value the given value.632 * @param excludeFields fields to exclude, if field does not exist it is ignored.633 * @return <code>null</code>.634 */635 public static <T> T refEq(T value, String... excludeFields) {636 reportMatcher(new ReflectionEquals(value, excludeFields));637 return null;638 }639 /**640 * Object argument that is the same as the given value.641 *642 * <p>643 * See examples in javadoc for {@link ArgumentMatchers} class644 * </p>645 *646 * @param <T> the type of the object, it is passed through to prevent casts.647 * @param value the given value.648 * @return <code>null</code>.649 */650 public static <T> T same(T value) {651 reportMatcher(new Same(value));652 if (value == null) {653 return null;654 }655 return (T) Primitives.defaultValue(value.getClass());656 }657 /**658 * <code>null</code> argument.659 *660 * <p>661 * See examples in javadoc for {@link ArgumentMatchers} class662 * </p>663 *664 * @return <code>null</code>.665 * @see #isNotNull()666 */667 public static <T> T isNull() {668 reportMatcher(Null.NULL);669 return null;670 }671 /**672 * Not <code>null</code> argument.673 *674 * <p>675 * Alias to {@link ArgumentMatchers#isNotNull()}676 * </p>677 *678 * <p>679 * See examples in javadoc for {@link ArgumentMatchers} class680 * </p>681 *682 * @return <code>null</code>.683 */684 public static <T> T notNull() {685 reportMatcher(NotNull.NOT_NULL);686 return null;687 }688 /**689 * Not <code>null</code> argument.690 *691 * <p>692 * Alias to {@link ArgumentMatchers#notNull()}693 * </p>694 *695 * <p>696 * See examples in javadoc for {@link ArgumentMatchers} class697 * </p>698 *699 * @return <code>null</code>.700 * @see #isNull()701 */702 public static <T> T isNotNull() {703 return notNull();704 }705 /**706 * Argument that is either <code>null</code> or of the given type.707 *708 * <p>709 * See examples in javadoc for {@link ArgumentMatchers} class710 * </p>711 *712 * @param clazz Type to avoid casting713 * @return <code>null</code>.714 */715 public static <T> T nullable(Class<T> clazz) {716 AdditionalMatchers.or(isNull(), isA(clazz));717 return Primitives.defaultValue(clazz);718 }719 /**720 * <code>String</code> argument that contains the given substring.721 * <p>722 * See examples in javadoc for {@link ArgumentMatchers} class723 *724 * @param substring the substring.725 * @return empty String ("").726 */727 public static String contains(String substring) {728 reportMatcher(new Contains(substring));729 return "";730 }731 /**732 * <code>String</code> argument that matches the given regular expression.733 * <p>734 * See examples in javadoc for {@link ArgumentMatchers} class735 *736 * @param regex the regular expression.737 * @return empty String ("").738 *739 * @see AdditionalMatchers#not(boolean)740 */741 public static String matches(String regex) {742 reportMatcher(new Matches(regex));743 return "";744 }745 /**746 * <code>Pattern</code> argument that matches the given regular expression.747 * <p>748 * See examples in javadoc for {@link ArgumentMatchers} class749 *750 * @param pattern the regular expression pattern.751 * @return empty String ("").752 *753 * @see AdditionalMatchers#not(boolean)754 */755 public static String matches(Pattern pattern) {756 reportMatcher(new Matches(pattern));757 return "";758 }759 /**760 * <code>String</code> argument that ends with the given suffix.761 * <p>762 * See examples in javadoc for {@link ArgumentMatchers} class763 *764 * @param suffix the suffix.765 * @return empty String ("").766 */767 public static String endsWith(String suffix) {768 reportMatcher(new EndsWith(suffix));769 return "";770 }771 /**772 * <code>String</code> argument that starts with the given prefix.773 * <p>774 * See examples in javadoc for {@link ArgumentMatchers} class775 *776 * @param prefix the prefix.777 * @return empty String ("").778 */779 public static String startsWith(String prefix) {780 reportMatcher(new StartsWith(prefix));781 return "";782 }783 /**784 * Allows creating custom argument matchers.785 *786 * <p>787 * This API has changed in 2.1.0, please read {@link ArgumentMatcher} for rationale and migration guide.788 * <b>NullPointerException</b> auto-unboxing caveat is described below.789 * </p>790 *791 * <p>792 * It is important to understand the use cases and available options for dealing with non-trivial arguments793 * <b>before</b> implementing custom argument matchers. This way, you can select the best possible approach794 * for given scenario and produce highest quality test (clean and maintainable).795 * Please read the documentation for {@link ArgumentMatcher} to learn about approaches and see the examples.796 * </p>797 *798 * <p>799 * <b>NullPointerException</b> auto-unboxing caveat.800 * In rare cases when matching primitive parameter types you <b>*must*</b> use relevant intThat(), floatThat(), etc. method.801 * This way you will avoid <code>NullPointerException</code> during auto-unboxing.802 * Due to how java works we don't really have a clean way of detecting this scenario and protecting the user from this problem.803 * Hopefully, the javadoc describes the problem and solution well.804 * If you have an idea how to fix the problem, let us know via the mailing list or the issue tracker.805 * </p>806 *807 * <p>808 * See examples in javadoc for {@link ArgumentMatcher} class809 * </p>810 *811 * @param matcher decides whether argument matches812 * @return <code>null</code>.813 */814 public static <T> T argThat(ArgumentMatcher<T> matcher) {815 reportMatcher(matcher);816 return null;817 }818 /**819 * Allows creating custom <code>char</code> argument matchers.820 *821 * Note that {@link #argThat} will not work with primitive <code>char</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.822 * <p>823 * See examples in javadoc for {@link ArgumentMatchers} class824 *825 * @param matcher decides whether argument matches826 * @return <code>0</code>.827 */828 public static char charThat(ArgumentMatcher<Character> matcher) {829 reportMatcher(matcher);830 return 0;831 }832 /**833 * Allows creating custom <code>boolean</code> argument matchers.834 *835 * Note that {@link #argThat} will not work with primitive <code>boolean</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.836 * <p>837 * See examples in javadoc for {@link ArgumentMatchers} class838 *839 * @param matcher decides whether argument matches840 * @return <code>false</code>.841 */842 public static boolean booleanThat(ArgumentMatcher<Boolean> matcher) {843 reportMatcher(matcher);844 return false;845 }846 /**847 * Allows creating custom <code>byte</code> argument matchers.848 *849 * Note that {@link #argThat} will not work with primitive <code>byte</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.850 * <p>851 * See examples in javadoc for {@link ArgumentMatchers} class852 *853 * @param matcher decides whether argument matches854 * @return <code>0</code>.855 */856 public static byte byteThat(ArgumentMatcher<Byte> matcher) {857 reportMatcher(matcher);858 return 0;859 }860 /**861 * Allows creating custom <code>short</code> argument matchers.862 *863 * Note that {@link #argThat} will not work with primitive <code>short</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.864 * <p>865 * See examples in javadoc for {@link ArgumentMatchers} class866 *867 * @param matcher decides whether argument matches868 * @return <code>0</code>.869 */870 public static short shortThat(ArgumentMatcher<Short> matcher) {871 reportMatcher(matcher);872 return 0;873 }874 /**875 * Allows creating custom <code>int</code> argument matchers.876 *877 * Note that {@link #argThat} will not work with primitive <code>int</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.878 * <p>879 * See examples in javadoc for {@link ArgumentMatchers} class880 *881 * @param matcher decides whether argument matches882 * @return <code>0</code>.883 */884 public static int intThat(ArgumentMatcher<Integer> matcher) {885 reportMatcher(matcher);886 return 0;887 }888 /**889 * Allows creating custom <code>long</code> argument matchers.890 *891 * Note that {@link #argThat} will not work with primitive <code>long</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.892 * <p>893 * See examples in javadoc for {@link ArgumentMatchers} class894 *895 * @param matcher decides whether argument matches896 * @return <code>0</code>.897 */898 public static long longThat(ArgumentMatcher<Long> matcher) {899 reportMatcher(matcher);900 return 0;901 }902 /**903 * Allows creating custom <code>float</code> argument matchers.904 *905 * Note that {@link #argThat} will not work with primitive <code>float</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.906 * <p>907 * See examples in javadoc for {@link ArgumentMatchers} class908 *909 * @param matcher decides whether argument matches910 * @return <code>0</code>.911 */912 public static float floatThat(ArgumentMatcher<Float> matcher) {913 reportMatcher(matcher);914 return 0;915 }916 /**917 * Allows creating custom <code>double</code> argument matchers.918 *919 * Note that {@link #argThat} will not work with primitive <code>double</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.920 * <p>921 * See examples in javadoc for {@link ArgumentMatchers} class922 *923 * @param matcher decides whether argument matches924 * @return <code>0</code>.925 */926 public static double doubleThat(ArgumentMatcher<Double> matcher) {927 reportMatcher(matcher);928 return 0;929 }930 private static void reportMatcher(ArgumentMatcher<?> matcher) {931 mockingProgress().getArgumentMatcherStorage().reportMatcher(matcher);932 }933}...
reportMatcher
Using AI Code Generation
1import static org.mockito.ArgumentMatchers.anyString;2import static org.mockito.ArgumentMatchers.argThat;3import java.util.Arrays;4import java.util.List;5import org.junit.Assert;6import org.junit.Test;7import org.mockito.ArgumentMatcher;8import org.mockito.Mockito;9public class MockitoArgumentMatchersTest {10 public void testArgumentMatchers() {11 List<String> mockedList = Mockito.mock(List.class);12 Mockito.when(mockedList.get(anyInt())).thenReturn("first", "second", "third");13 Assert.assertEquals("first", mockedList.get(1));14 Assert.assertEquals("second", mockedList.get(2));15 Assert.assertEquals("third", mockedList.get(3));16 }17 public void testArgumentMatchersWithCustomMatcher() {18 List<String> mockedList = Mockito.mock(List.class);19 Mockito.when(mockedList.get(argThat(new ArgumentMatcher<Integer>() {20 public boolean matches(Integer argument) {21 return argument > 0 && argument < 5;22 }23 }))).thenReturn("first", "second", "third");24 Assert.assertEquals("first", mockedList.get(1));25 Assert.assertEquals("second", mockedList.get(2));26 Assert.assertEquals("third", mockedList.get(3));27 }28 public void testArgumentMatchersWithCustomMatcherUsingLambda() {29 List<String> mockedList = Mockito.mock(List.class);30 Mockito.when(mockedList.get(argThat((Integer argument) -> {31 return argument > 0 && argument < 5;32 }))).thenReturn("first", "second", "third");33 Assert.assertEquals("first", mockedList.get(1));34 Assert.assertEquals("second", mockedList.get(2));35 Assert.assertEquals("third", mockedList.get(3));36 }37 public void testArgumentMatchersWithCustomMatcherUsingLambdaAndMethodReference() {38 List<String> mockedList = Mockito.mock(List.class);39 Mockito.when(mockedList.get(argThat(MockitoArgumentMatchersTest::isBetween1And4))).thenReturn("first", "second", "third");40 Assert.assertEquals("first", mockedList.get(1));41 Assert.assertEquals("second", mockedList.get(2));42 Assert.assertEquals("third", mockedList.get(3));43 }44 private static boolean isBetween1And4(Integer argument) {45 return argument > 0 && argument < 5;46 }
reportMatcher
Using AI Code Generation
1package com.baeldung.mockito;2import static org.junit.jupiter.api.Assertions.assertEquals;3import static org.mockito.ArgumentMatchers.anyInt;4import static org.mockito.ArgumentMatchers.anyString;5import static org.mockito.ArgumentMatchers.eq;6import static org.mockito.ArgumentMatchers.isA;7import static org.mockito.ArgumentMatchers.isNull;8import static org.mockito.ArgumentMatchers.matches;9import static org.mockito.ArgumentMatchers.notNull;10import static org.mockito.ArgumentMatchers.startsWith;11import static org.mockito.Mockito.mock;12import static org.mockito.Mockito.when;13import java.util.List;14import org.junit.jupiter.api.Test;15public class MatchersUnitTest {16 public void givenAnyInt_whenMocking_thenCorrect() {17 List mockedList = mock(List.class);18 when(mockedList.get(anyInt())).thenReturn("element");19 assertEquals("element", mockedList.get(999));20 }21 public void givenAnyString_whenMocking_thenCorrect() {22 List mockedList = mock(List.class);23 when(mockedList.contains(anyString())).thenReturn(true);24 assertEquals(true, mockedList.contains("element"));25 }26 public void givenEq_whenMocking_thenCorrect() {27 List mockedList = mock(List.class);28 when(mockedList.get(eq(1))).thenReturn("element");29 assertEquals("element", mockedList.get(1));30 }31 public void givenIsA_whenMocking_thenCorrect() {32 List mockedList = mock(List.class);33 when(mockedList.get(isA(Integer.class))).thenReturn("element");34 assertEquals("element", mockedList.get(1));35 }36 public void givenIsNull_whenMocking_thenCorrect() {37 List mockedList = mock(List.class);38 when(mockedList.get(isNull())).thenReturn("element");39 assertEquals("element", mockedList.get(null));40 }41 public void givenNotNull_whenMocking_thenCorrect() {42 List mockedList = mock(List.class);43 when(mockedList.get(notNull())).thenReturn("element");44 assertEquals("element", mockedList.get(1));45 }46 public void givenMatches_whenMocking_thenCorrect() {47 List mockedList = mock(List.class);48 when(mockedList.get(matches("\\d+"))).thenReturn("element");49 assertEquals("element", mockedList.get(1));50 }51 public void givenStartsWith_whenMocking_thenCorrect() {52 List mockedList = mock(List.class);
reportMatcher
Using AI Code Generation
1import static org.mockito.ArgumentMatchers.*;2import static org.mockito.Mockito.*;3public class MockitoTest {4 public static void main(String[] args) {5 List<String> mockedList = mock(List.class);6 when(mockedList.get(anyInt())).thenReturn("element");7 when(mockedList.contains(argThat(isValid()))).thenReturn(true);8 System.out.println(mockedList.get(999));9 verify(mockedList).get(anyInt());10 verify(mockedList).add(argThat(s -> s.length() > 5));11 }12 private static ArgumentMatcher<String> isValid() {13 return new ArgumentMatcher<String>() {14 public boolean matches(String argument) {15 return argument != null && argument.length() > 5;16 }17 };18 }19}20import org.junit.Test;21import org.mockito.ArgumentCaptor;22import org.mockito.Mockito;23import java.util.List;24import static org.junit.Assert.assertEquals;25import static org.mockito.Mockito.*;26public class MockitoArgumentCaptorTest {27 public void argumentCaptorTest() {28 List mockedList = mock(List.class);29 mockedList.add("one");30 mockedList.add("two");31 ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);32 verify(mockedList, times(2)).add(argumentCaptor.capture());33 List<String> capturedArguments = argumentCaptor.getAllValues();34 assertEquals("one", capturedArguments.get(0));35 assertEquals("two", capturedArguments.get(1));36 }37}
reportMatcher
Using AI Code Generation
1import static org.mockito.ArgumentMatchers.*;2import static org.mockito.Matchers.*;3import static org.mockito.Mockito.*;4import java.util.LinkedList;5import org.junit.Test;6public class MockitoTest {7 public void test() {8 LinkedList mockedList = mock(LinkedList.class);9 mockedList.add("one");10 mockedList.clear();11 verify(mockedList).add("one");12 verify(mockedList).clear();13 }14}15OK (1 test)
reportMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2import org.mockito.Mockito;3class CustomMatcher implements ArgumentMatcher<Integer> {4 public boolean matches(Integer i) {5 return i > 10;6 }7}8public class CustomMatcherDemo {9 public static void main(String[] args) {10 CustomMatcher cm = new CustomMatcher();11 Mockito.when(cm.matches(Mockito.argThat(cm))).thenReturn(true);12 }13}14import org.mockito.ArgumentMatcher;15import org.mockito.Mockito;16class CustomMatcher implements ArgumentMatcher<Integer> {17 public boolean matches(Integer i) {18 return i > 10;19 }20}21public class CustomMatcherDemo {22 public static void main(String[] args)
reportMatcher
Using AI Code Generation
1when(mockedClass.method(any(Matcher.class))).thenReturn("some value");2when(mockedClass.method(any(Matcher.class))).thenReturn(null);3when(mockedClass.method(any(Matcher.class))).thenReturn(true);4when(mockedClass.method(any(Matcher.class))).thenReturn(false);5when(mockedClass.method(any(Matcher.class))).thenReturn((byte) 1);6when(mockedClass.method(any(Matcher.class))).thenReturn((byte) 0);7when(mockedClass.method(any(Matcher.class))).thenReturn((byte) -1);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!