cloudstack/tools/mockito/javadoc/org/mockito/ArgumentMatcher.html
2011-03-19 11:46:18 -07:00

339 lines
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.5.0_22) on Mon May 24 23:03:13 CEST 2010 -->
<TITLE>
ArgumentMatcher (Mockito API)
</TITLE>
<META NAME="keywords" CONTENT="org.mockito.ArgumentMatcher class">
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
parent.document.title="ArgumentMatcher (Mockito API)";
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<BODY BGCOLOR="white" onload="windowTitle();">
<!-- ========= START OF TOP NAVBAR ======= -->
<A NAME="navbar_top"><!-- --></A>
<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/ArgumentMatcher.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
&nbsp;<A HREF="../../org/mockito/ArgumentCaptor.html" title="class in org.mockito"><B>PREV CLASS</B></A>&nbsp;
&nbsp;<A HREF="../../org/mockito/BDDMockito.html" title="class in org.mockito"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../index.html?org/mockito/ArgumentMatcher.html" target="_top"><B>FRAMES</B></A> &nbsp;
&nbsp;<A HREF="ArgumentMatcher.html" target="_top"><B>NO FRAMES</B></A> &nbsp;
&nbsp;<SCRIPT type="text/javascript">
<!--
if(window==top) {
document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
}
//-->
</SCRIPT>
<NOSCRIPT>
<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>
</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_top"></A>
<!-- ========= END OF TOP NAVBAR ========= -->
<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
<FONT SIZE="-1">
org.mockito</FONT>
<BR>
Class ArgumentMatcher&lt;T&gt;</H2>
<PRE>
java.lang.Object
<IMG SRC="../../resources/inherit.gif" ALT="extended by ">org.hamcrest.BaseMatcher&lt;T&gt;
<IMG SRC="../../resources/inherit.gif" ALT="extended by "><B>org.mockito.ArgumentMatcher&lt;T&gt;</B>
</PRE>
<DL>
<DT><DT><B>Type Parameters:</B><DD><CODE>T</CODE> - type of argument</DL>
<DL>
<DT><B>All Implemented Interfaces:</B> <DD>org.hamcrest.Matcher&lt;T&gt;, org.hamcrest.SelfDescribing</DD>
</DL>
<DL>
<DT><B>Direct Known Subclasses:</B> <DD><A HREF="../../org/mockito/internal/matchers/And.html" title="class in org.mockito.internal.matchers">And</A>, <A HREF="../../org/mockito/internal/matchers/Any.html" title="class in org.mockito.internal.matchers">Any</A>, <A HREF="../../org/mockito/internal/matchers/AnyVararg.html" title="class in org.mockito.internal.matchers">AnyVararg</A>, <A HREF="../../org/mockito/internal/matchers/CapturingMatcher.html" title="class in org.mockito.internal.matchers">CapturingMatcher</A>, <A HREF="../../org/mockito/internal/matchers/CompareTo.html" title="class in org.mockito.internal.matchers">CompareTo</A>, <A HREF="../../org/mockito/internal/matchers/Contains.html" title="class in org.mockito.internal.matchers">Contains</A>, <A HREF="../../org/mockito/internal/matchers/EndsWith.html" title="class in org.mockito.internal.matchers">EndsWith</A>, <A HREF="../../org/mockito/internal/matchers/Equals.html" title="class in org.mockito.internal.matchers">Equals</A>, <A HREF="../../org/mockito/internal/matchers/EqualsWithDelta.html" title="class in org.mockito.internal.matchers">EqualsWithDelta</A>, <A HREF="../../org/mockito/internal/matchers/Find.html" title="class in org.mockito.internal.matchers">Find</A>, <A HREF="../../org/mockito/internal/matchers/InstanceOf.html" title="class in org.mockito.internal.matchers">InstanceOf</A>, <A HREF="../../org/mockito/internal/matchers/Matches.html" title="class in org.mockito.internal.matchers">Matches</A>, <A HREF="../../org/mockito/internal/matchers/Not.html" title="class in org.mockito.internal.matchers">Not</A>, <A HREF="../../org/mockito/internal/matchers/NotNull.html" title="class in org.mockito.internal.matchers">NotNull</A>, <A HREF="../../org/mockito/internal/matchers/Null.html" title="class in org.mockito.internal.matchers">Null</A>, <A HREF="../../org/mockito/internal/matchers/Or.html" title="class in org.mockito.internal.matchers">Or</A>, <A HREF="../../org/mockito/internal/matchers/apachecommons/ReflectionEquals.html" title="class in org.mockito.internal.matchers.apachecommons">ReflectionEquals</A>, <A HREF="../../org/mockito/internal/matchers/Same.html" title="class in org.mockito.internal.matchers">Same</A>, <A HREF="../../org/mockito/internal/matchers/StartsWith.html" title="class in org.mockito.internal.matchers">StartsWith</A></DD>
</DL>
<HR>
<DL>
<DT><PRE>public abstract class <B>ArgumentMatcher&lt;T&gt;</B><DT>extends org.hamcrest.BaseMatcher&lt;T&gt;</DL>
</PRE>
<P>
Allows creating customized argument matchers.
<p>
ArgumentMatcher is an hamcrest <CODE>Matcher</CODE> with predefined describeTo() method.
In case of failure, ArgumentMatcher generates description based on <b>decamelized class name</b> - to promote meaningful class names.
For example <b>StringWithStrongLanguage</b> matcher will generate 'String with strong language' description.
You can always override describeTo() method and provide detailed description.
<p>
Use <A HREF="../../org/mockito/Matchers.html#argThat(org.hamcrest.Matcher)"><CODE>Matchers.argThat(org.hamcrest.Matcher<T>)</CODE></A> method and pass an instance of hamcrest <CODE>Matcher</CODE>, e.g:
<pre>
class IsListOfTwoElements extends ArgumentMatcher&lt;List&gt; {
public boolean matches(Object list) {
return ((List) list).size() == 2;
}
}
List mock = mock(List.class);
when(mock.addAll(argThat(new IsListOfTwoElements()))).thenReturn(true);
mock.addAll(Arrays.asList(&quot;one&quot;, &quot;two&quot;));
verify(mock).addAll(argThat(new IsListOfTwoElements()));
</pre>
To keep it readable you may want to extract method, e.g:
<pre>
verify(mock).addAll(<b>argThat(new IsListOfTwoElements())</b>);
//becomes
verify(mock).addAll(<b>listOfTwoElements()</b>);
</pre>
<b>Warning:</b> Be reasonable with using complicated argument matching, especially custom argument matchers, as it can make the test less readable.
Sometimes it's better to implement equals() for arguments that are passed to mocks
(Mockito naturally uses equals() for argument matching).
This can make the test cleaner.
<p>
Also, <b>sometimes <A HREF="../../org/mockito/ArgumentCaptor.html" title="class in org.mockito"><CODE>ArgumentCaptor</CODE></A> may be a better fit</b> than custom matcher.
For example, if custom argument matcher is not likely to be reused
or you just need it to assert on argument values to complete verification of behavior.
<p>
Read more about other matchers in javadoc for <A HREF="../../org/mockito/Matchers.html" title="class in org.mockito"><CODE>Matchers</CODE></A> class
<P>
<P>
<HR>
<P>
<!-- ======== CONSTRUCTOR SUMMARY ======== -->
<A NAME="constructor_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Constructor Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE><B><A HREF="../../org/mockito/ArgumentMatcher.html#ArgumentMatcher()">ArgumentMatcher</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
</TABLE>
&nbsp;
<!-- ========== METHOD SUMMARY =========== -->
<A NAME="method_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Method Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../org/mockito/ArgumentMatcher.html#describeTo(org.hamcrest.Description)">describeTo</A></B>(org.hamcrest.Description&nbsp;description)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>abstract &nbsp;boolean</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../org/mockito/ArgumentMatcher.html#matches(java.lang.Object)">matches</A></B>(java.lang.Object&nbsp;argument)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns whether this matcher accepts the given argument.</TD>
</TR>
</TABLE>
&nbsp;<A NAME="methods_inherited_from_class_org.hamcrest.BaseMatcher"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
<TH ALIGN="left"><B>Methods inherited from class org.hamcrest.BaseMatcher</B></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE>_dont_implement_Matcher___instead_extend_BaseMatcher_, toString</CODE></TD>
</TR>
</TABLE>
&nbsp;<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
<TH ALIGN="left"><B>Methods inherited from class java.lang.Object</B></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD><CODE>clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait</CODE></TD>
</TR>
</TABLE>
&nbsp;
<P>
<!-- ========= CONSTRUCTOR DETAIL ======== -->
<A NAME="constructor_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Constructor Detail</B></FONT></TH>
</TR>
</TABLE>
<A NAME="ArgumentMatcher()"><!-- --></A><H3>
ArgumentMatcher</H3>
<PRE>
public <B>ArgumentMatcher</B>()</PRE>
<DL>
</DL>
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Method Detail</B></FONT></TH>
</TR>
</TABLE>
<A NAME="matches(java.lang.Object)"><!-- --></A><H3>
matches</H3>
<PRE>
public abstract boolean <B>matches</B>(java.lang.Object&nbsp;argument)</PRE>
<DL>
<DD>Returns whether this matcher accepts the given argument.
<p>
The method should <b>never</b> assert if the argument doesn't match. It
should only return false.
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>argument</CODE> - the argument
<DT><B>Returns:</B><DD>whether this matcher accepts the given argument.</DL>
</DD>
</DL>
<HR>
<A NAME="describeTo(org.hamcrest.Description)"><!-- --></A><H3>
describeTo</H3>
<PRE>
public void <B>describeTo</B>(org.hamcrest.Description&nbsp;description)</PRE>
<DL>
<DD><DL>
</DL>
</DD>
</DL>
<!-- ========= END OF CLASS DATA ========= -->
<HR>
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<A NAME="navbar_bottom"><!-- --></A>
<A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_bottom_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/ArgumentMatcher.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
&nbsp;<A HREF="../../org/mockito/ArgumentCaptor.html" title="class in org.mockito"><B>PREV CLASS</B></A>&nbsp;
&nbsp;<A HREF="../../org/mockito/BDDMockito.html" title="class in org.mockito"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../index.html?org/mockito/ArgumentMatcher.html" target="_top"><B>FRAMES</B></A> &nbsp;
&nbsp;<A HREF="ArgumentMatcher.html" target="_top"><B>NO FRAMES</B></A> &nbsp;
&nbsp;<SCRIPT type="text/javascript">
<!--
if(window==top) {
document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
}
//-->
</SCRIPT>
<NOSCRIPT>
<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>
</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_bottom"></A>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
<HR>
</BODY>
</HTML>