2011-03-19 11:46:18 -07:00

410 lines
17 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>
MockSettings (Mockito API)
</TITLE>
<META NAME="keywords" CONTENT="org.mockito.MockSettings interface">
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
parent.document.title="MockSettings (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/MockSettings.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/MockitoDebugger.html" title="interface in org.mockito"><B>PREV CLASS</B></A>&nbsp;
&nbsp;<A HREF="../../org/mockito/ReturnValues.html" title="interface in org.mockito"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../index.html?org/mockito/MockSettings.html" target="_top"><B>FRAMES</B></A> &nbsp;
&nbsp;<A HREF="MockSettings.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;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&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>
Interface MockSettings</H2>
<DL>
<DT><B>All Superinterfaces:</B> <DD>java.io.Serializable</DD>
</DL>
<DL>
<DT><B>All Known Implementing Classes:</B> <DD><A HREF="../../org/mockito/internal/creation/MockSettingsImpl.html" title="class in org.mockito.internal.creation">MockSettingsImpl</A></DD>
</DL>
<HR>
<DL>
<DT><PRE>public interface <B>MockSettings</B><DT>extends java.io.Serializable</DL>
</PRE>
<P>
Allows mock creation with additional mock settings.
<p>
Don't use it too often.
Consider writing simple tests that use simple mocks.
Repeat after me: simple tests push simple, KISSy, readable & maintainable code.
If you cannot write a test in a simple way - refactor the code under test.
<p>
Examples of mock settings:
<pre>
//Creates mock with different default answer & name
Foo mock = mock(Foo.class, withSettings()
.defaultAnswer(RETURNS_SMART_NULLS)
.name("cool mockie"));
//Creates mock with different default answer, descriptive name and extra interfaces
Foo mock = mock(Foo.class, withSettings()
.defaultAnswer(RETURNS_SMART_NULLS)
.name("cool mockie")
.extraInterfaces(Bar.class));
</pre>
<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito"><CODE>MockSettings</CODE></A> has been introduced for two reasons.
Firstly, to make it easy to add another mock setting when the demand comes.
Secondly, to enable combining together different mock settings without introducing zillions of overloaded mock() methods.
<P>
<P>
<HR>
<P>
<!-- ========== 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;<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A></CODE></FONT></TD>
<TD><CODE><B><A HREF="../../org/mockito/MockSettings.html#defaultAnswer(org.mockito.stubbing.Answer)">defaultAnswer</A></B>(<A HREF="../../org/mockito/stubbing/Answer.html" title="interface in org.mockito.stubbing">Answer</A>&nbsp;defaultAnswer)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Specifies default answers to interactions.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A></CODE></FONT></TD>
<TD><CODE><B><A HREF="../../org/mockito/MockSettings.html#extraInterfaces(java.lang.Class...)">extraInterfaces</A></B>(java.lang.Class&lt;?&gt;...&nbsp;interfaces)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Specifies extra interfaces the mock should implement.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A></CODE></FONT></TD>
<TD><CODE><B><A HREF="../../org/mockito/MockSettings.html#name(java.lang.String)">name</A></B>(java.lang.String&nbsp;name)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Specifies mock name.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A></CODE></FONT></TD>
<TD><CODE><B><A HREF="../../org/mockito/MockSettings.html#serializable()">serializable</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Configures the mock to be serializable.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A></CODE></FONT></TD>
<TD><CODE><B><A HREF="../../org/mockito/MockSettings.html#spiedInstance(java.lang.Object)">spiedInstance</A></B>(java.lang.Object&nbsp;instance)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Specifies the instance to spy on.</TD>
</TR>
</TABLE>
&nbsp;
<P>
<!-- ============ 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="extraInterfaces(java.lang.Class...)"><!-- --></A><H3>
extraInterfaces</H3>
<PRE>
<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A> <B>extraInterfaces</B>(java.lang.Class&lt;?&gt;...&nbsp;interfaces)</PRE>
<DL>
<DD>Specifies extra interfaces the mock should implement. Might be useful for legacy code or some corner cases.
For background, see issue 51 <a href="http://code.google.com/p/mockito/issues/detail?id=51">here</a>
<p>
This mysterious feature should be used very occasionally.
The object under test should know exactly its collaborators & dependencies.
If you happen to use it often than please make sure you are really producing simple, clean & readable code.
<p>
Examples:
<pre>
Foo foo = mock(Foo.class, withSettings().extraInterfaces(Bar.class, Baz.class));
//now, the mock implements extra interfaces, so following casting is possible:
Bar bar = (Bar) foo;
Baz baz = (Baz) foo;
</pre>
<P>
<DD><DL>
</DL>
</DD>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>interfaces</CODE> - extra interfaces the should implement.
<DT><B>Returns:</B><DD>settings instance so that you can fluently specify other settings</DL>
</DD>
</DL>
<HR>
<A NAME="name(java.lang.String)"><!-- --></A><H3>
name</H3>
<PRE>
<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A> <B>name</B>(java.lang.String&nbsp;name)</PRE>
<DL>
<DD>Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors.
<p>
Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators.
<b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks.
<p>
<b>If you use &#064;Mock annotation then you've got naming mocks for free!</b> &#064;Mock uses field name as mock name. <A HREF="../../org/mockito/Mock.html" title="annotation in org.mockito"><CODE>Read more.</CODE></A>
<p>
Examples:
<pre>
Foo foo = mock(Foo.class, withSettings().name("foo"));
//Below does exactly the same:
Foo foo = mock(Foo.class, "foo");
</pre>
<P>
<DD><DL>
</DL>
</DD>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>name</CODE> - the name of the mock, later used in all verification errors
<DT><B>Returns:</B><DD>settings instance so that you can fluently specify other settings</DL>
</DD>
</DL>
<HR>
<A NAME="spiedInstance(java.lang.Object)"><!-- --></A><H3>
spiedInstance</H3>
<PRE>
<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A> <B>spiedInstance</B>(java.lang.Object&nbsp;instance)</PRE>
<DL>
<DD>Specifies the instance to spy on. Makes sense only for spies/partial mocks.
Sets the real implementation to be called when the method is called on a mock object.
<p>
As usual you are going to read <b>the partial mock warning</b>:
Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects.
How does partial mock fit into this paradigm? Well, it just doesn't...
Partial mock usually means that the complexity has been moved to a different method on the same object.
In most cases, this is not the way you want to design your application.
<p>
However, there are rare cases when partial mocks come handy:
dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.)
However, I wouldn't use partial mocks for new, test-driven & well-designed code.
<p>
Enough warnings about partial mocks, see an example how spiedInstance() works:
<pre>
Foo foo = mock(Foo.class, spiedInstance(fooInstance));
//Below does exactly the same:
Foo foo = spy(fooInstance);
</pre>
<P>
<DD><DL>
</DL>
</DD>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>instance</CODE> - to spy on
<DT><B>Returns:</B><DD>settings instance so that you can fluently specify other settings</DL>
</DD>
</DL>
<HR>
<A NAME="defaultAnswer(org.mockito.stubbing.Answer)"><!-- --></A><H3>
defaultAnswer</H3>
<PRE>
<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A> <B>defaultAnswer</B>(<A HREF="../../org/mockito/stubbing/Answer.html" title="interface in org.mockito.stubbing">Answer</A>&nbsp;defaultAnswer)</PRE>
<DL>
<DD>Specifies default answers to interactions.
It's quite advanced feature and typically you don't need it to write decent tests.
However it can be helpful when working with legacy systems.
<p>
It is the default answer so it will be used <b>only when you don't</b> stub the method call.
<pre>
Foo mock = mock(Foo.class, withSettings().defaultAnswer(RETURNS_SMART_NULLS));
Foo mockTwo = mock(Foo.class, withSettings().defaultAnswer(new YourOwnAnswer()));
//Below does exactly the same:
Foo mockTwo = mock(Foo.class, new YourOwnAnswer());
</pre>
<P>
<DD><DL>
</DL>
</DD>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>defaultAnswer</CODE> - default answer to be used by mock when not stubbed
<DT><B>Returns:</B><DD>settings instance so that you can fluently specify other settings</DL>
</DD>
</DL>
<HR>
<A NAME="serializable()"><!-- --></A><H3>
serializable</H3>
<PRE>
<A HREF="../../org/mockito/MockSettings.html" title="interface in org.mockito">MockSettings</A> <B>serializable</B>()</PRE>
<DL>
<DD>Configures the mock to be serializable. With this feature you can use a mock in a place that requires dependencies to be serializable.
<p>
WARNING: This should be rarely used in unit testing.
<p>
The behaviour was implemented for a specific use case of a BDD spec that had an unreliable external dependency. This
was in a web environment and the objects from the external dependency were being serialized to pass between layers.
<p>
Example:
<pre>
List serializableMock = mock(List.class, withSettings().serializable());
</pre>
<P>
<DD><DL>
</DL>
</DD>
<DD><DL>
<DT><B>Returns:</B><DD>settings instance so that you can fluently specify other settings</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/MockSettings.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/MockitoDebugger.html" title="interface in org.mockito"><B>PREV CLASS</B></A>&nbsp;
&nbsp;<A HREF="../../org/mockito/ReturnValues.html" title="interface in org.mockito"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../index.html?org/mockito/MockSettings.html" target="_top"><B>FRAMES</B></A> &nbsp;
&nbsp;<A HREF="MockSettings.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;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&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>