<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog #6 &#8211; CS@Worcester</title>
	<atom:link href="https://cs.worcester.edu/category/blog-6/feed/" rel="self" type="application/rss+xml" />
	<link>https://cs.worcester.edu</link>
	<description>Worcester State University Computer Science Department</description>
	<lastBuildDate>Fri, 30 Apr 2021 14:38:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
<site xmlns="com-wordpress:feed-additions:1">236835116</site>	<item>
		<title>Detailed and basic usage of Mockito</title>
		<link>https://haorusong.blogspot.com/2021/04/detailed-and-basic-usage-of-mockito.html</link>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Fri, 30 Apr 2021 14:38:00 +0000</pubDate>
				<category><![CDATA[#CS]]></category>
		<category><![CDATA[blog #6]]></category>
		<category><![CDATA[CS-443]]></category>
		<category><![CDATA[CS@Worcester]]></category>
		<guid isPermaLink="false">http://cs.worcester.edu/blog/?guid=edb7057eb4b7f79269c2b31982134483</guid>

					<description><![CDATA[&#160;Mockito is a Mock framework for Java single-testing, but it can also be used with other single-testing frameworks in addition to JUnit. Mockito changes the behavior of a class or object, allowing us to focus more on testing the code logic without...]]></description>
										<content:encoded><![CDATA[<p>&nbsp;Mockito is a Mock framework for Java single-testing, but it can also be used with other single-testing frameworks in addition to JUnit. Mockito changes the behavior of a class or object, allowing us to focus more on testing the code logic without the effort of constructing the data.</p>
<p></p>
<p>The basic concept</p>
<p>Mocks can be of two types, Class and Partial，so Mockito is called spy. The behavior of changing methods on mock objects is called Stub.</p>
<p>A Mock process is called a Mock Session, and it records all the Stubbing. It consists of three steps:</p>
<p></p>
<p><span style="background-color: #f7f7f7; color: #4d4d4c; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; white-space: pre;">+&#8212;&#8212;&#8212;-+      +&#8212;&#8212;+      +&#8212;&#8212;&#8211;+ | Mock/Spy | ===&gt; | Stub | ===&gt; | Verify | +&#8212;&#8212;&#8212;-+      +&#8212;&#8212;+      +&#8212;&#8212;&#8211;+</span></p>
<p></p>
<p></p>
<p>Class Mock</p>
<p>A Class Mock changes the behavior of a Class so that the object it mocks completely loses its original behavior.</p>
<p>Method returns default values (null, false, 0, etc.) if it is not pegged.</p>
<p></p>
<p>The most basic usage is as follows:</p>
<table class="table" style="background-color: #f7f7f7; border-collapse: collapse; border-spacing: 0px; border: none; color: #4d4d4c; font-family: -apple-system, &quot;Helvetica Neue&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, STHeiti, &quot;Microsoft YaHei&quot;, &quot;Microsoft JhengHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;Source Han Sans CN&quot;, &quot;Noto Sans SC&quot;, &quot;Source Han Sans TC&quot;, &quot;Noto Sans CJK TC&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 13px; margin: 0px; max-width: 100%; width: auto;">
<tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;">
<td class="gutter" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; color: #666666; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px 20px 1px 1px; text-align: right; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;">1</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">2</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">3</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">4</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">5</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">6</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">7</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">8</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">9</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">10</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">11</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">12</span><br style="box-sizing: border-box;" /></pre>
</td>
<td class="code" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="keyword" style="box-sizing: border-box; color: #8959a8;">import</span> <span class="keyword" style="box-sizing: border-box; color: #8959a8;">static</span> org.mockito.Mockito.*;</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// use List.class to creat a mock subject --- mockedList</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">List mockedList = mock(List.class);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /></p>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">//operation of mockedList</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">mockedList.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">mockedList.clear();</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /></p>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">//validation</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">verify(mockedList).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(mockedList).clear();</span></td>
</tr>
</tbody>
</table>
<p>Partial Mock（spy）</p>
<p>If we only want to change the behavior of an instance, we need to use spy:</p>
<table class="table" style="background-color: #f7f7f7; border-collapse: collapse; border-spacing: 0px; border: none; color: #4d4d4c; font-family: -apple-system, &quot;Helvetica Neue&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, STHeiti, &quot;Microsoft YaHei&quot;, &quot;Microsoft JhengHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;Source Han Sans CN&quot;, &quot;Noto Sans SC&quot;, &quot;Source Han Sans TC&quot;, &quot;Noto Sans CJK TC&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 13px; margin: 0px; max-width: 100%; width: auto;">
<tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;">
<td class="gutter" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; color: #666666; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px 20px 1px 1px; text-align: right; word-break: break-all;">1<br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">2</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">3</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">4</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">5</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">6</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">7</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">8</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">9</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">10</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">11</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">12</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">13</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">14</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">15</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">16</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">17</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">18</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">19</span><br style="box-sizing: border-box;" /></pre>
</td>
<td class="code" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;">List list = <span class="keyword" style="box-sizing: border-box; color: #8959a8;">new</span> LinkedList();</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">List spy = spy(list);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// optionally, you can stub out some methods:</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">when(spy.size()).thenReturn(<span class="number" style="box-sizing: border-box; color: #718c00;">100</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// using the spy calls *real* methods</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">spy.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">spy.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"two"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// prints "one" - the first element of a list</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">System.out.println(spy.get(<span class="number" style="box-sizing: border-box; color: #718c00;">0</span>));</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// size() method was stubbed - 100 is printed</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">System.out.println(spy.size());</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// optionally, you can verify</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(spy).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(spy).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"two"</span>);</span></pre>
</td>
</tr>
</tbody>
</table>
<p></p>
<p>As you can see from the code, the main difference between a Spy and a MockSettings is that the MockSettings for a Spy needs to be passed in a SpiedInstance.</p>
<p></p>
<p>The default Answer to a spy is CALLS_REAL_METHODS, which means that if a method is not stub, it performs its real behavior.</p>
<p>The default Answer to a mock is RETURNS_DEFAULTS. Methods that are not stub return a default value.</p>
<p></p>
<p>source：</p>
<p>https://www.vogella.com/tutorials/Mockito/article.html</p>
<p>https://howtodoinjava.com/mockito/junit-mockito-example/</p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>

<p class="syndicated-attribution"><em>From the blog <a href="https://haorusong.blogspot.com/search/label/CS%40Worcester">haorusong</a> by <a href="https://cs.worcester.edu/author/0/" title="Read other posts by "></a></em> and used with permission of the author. All other rights reserved by the author.</p>]]></content:encoded>
					
		
		<enclosure url="" length="0" type="" />

		<post-id xmlns="com-wordpress:feed-additions:1">15178</post-id>	</item>
		<item>
		<title>Detailed and basic usage of Mockito</title>
		<link>https://haorusong.blogspot.com/2021/04/detailed-and-basic-usage-of-mockito.html</link>
		
		<dc:creator><![CDATA[Unknown]]></dc:creator>
		<pubDate>Fri, 30 Apr 2021 14:38:00 +0000</pubDate>
				<category><![CDATA[#CS]]></category>
		<category><![CDATA[blog #6]]></category>
		<category><![CDATA[CS-443]]></category>
		<category><![CDATA[CS@Worcester]]></category>
		<guid isPermaLink="false">http://cs.worcester.edu/?guid=edb7057eb4b7f79269c2b31982134483</guid>

					<description><![CDATA[&#160;Mockito is a Mock framework for Java single-testing, but it can also be used with other single-testing frameworks in addition to JUnit. Mockito changes the behavior of a class or object, allowing us to focus more on testing the code logic without...]]></description>
										<content:encoded><![CDATA[<p>&nbsp;Mockito is a Mock framework for Java single-testing, but it can also be used with other single-testing frameworks in addition to JUnit. Mockito changes the behavior of a class or object, allowing us to focus more on testing the code logic without the effort of constructing the data.</p>
<p></p>
<p>The basic concept</p>
<p>Mocks can be of two types, Class and Partial，so Mockito is called spy. The behavior of changing methods on mock objects is called Stub.</p>
<p>A Mock process is called a Mock Session, and it records all the Stubbing. It consists of three steps:</p>
<p></p>
<p><span style="background-color: #f7f7f7; color: #4d4d4c; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; white-space: pre;">+&#8212;&#8212;&#8212;-+      +&#8212;&#8212;+      +&#8212;&#8212;&#8211;+<br />
| Mock/Spy | ===&gt; | Stub | ===&gt; | Verify |<br />
+&#8212;&#8212;&#8212;-+      +&#8212;&#8212;+      +&#8212;&#8212;&#8211;+</span></p>
<p></p>
<p></p>
<p>Class Mock</p>
<p>A Class Mock changes the behavior of a Class so that the object it mocks completely loses its original behavior.</p>
<p>Method returns default values (null, false, 0, etc.) if it is not pegged.</p>
<p></p>
<p>The most basic usage is as follows:</p>
<table class="table" style="background-color: #f7f7f7; border-collapse: collapse; border-spacing: 0px; border: none; color: #4d4d4c; font-family: -apple-system, &quot;Helvetica Neue&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, STHeiti, &quot;Microsoft YaHei&quot;, &quot;Microsoft JhengHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;Source Han Sans CN&quot;, &quot;Noto Sans SC&quot;, &quot;Source Han Sans TC&quot;, &quot;Noto Sans CJK TC&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 13px; margin: 0px; max-width: 100%; width: auto;">
<tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;">
<td class="gutter" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; color: #666666; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px 20px 1px 1px; text-align: right; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;">1</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">2</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">3</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">4</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">5</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">6</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">7</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">8</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">9</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">10</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">11</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">12</span><br style="box-sizing: border-box;" /></pre>
</td>
<td class="code" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="keyword" style="box-sizing: border-box; color: #8959a8;">import</span> <span class="keyword" style="box-sizing: border-box; color: #8959a8;">static</span> org.mockito.Mockito.*;</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// use List.class to creat a mock subject --- mockedList</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">List mockedList = mock(List.class);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /></p>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">//operation of mockedList</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">mockedList.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">mockedList.clear();</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /></p>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">//validation</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">verify(mockedList).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(mockedList).clear();</span></td>
</tr>
</tbody>
</table>
<p>Partial Mock（spy）</p>
<p>If we only want to change the behavior of an instance, we need to use spy:</p>
<table class="table" style="background-color: #f7f7f7; border-collapse: collapse; border-spacing: 0px; border: none; color: #4d4d4c; font-family: -apple-system, &quot;Helvetica Neue&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, STHeiti, &quot;Microsoft YaHei&quot;, &quot;Microsoft JhengHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;Source Han Sans CN&quot;, &quot;Noto Sans SC&quot;, &quot;Source Han Sans TC&quot;, &quot;Noto Sans CJK TC&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 13px; margin: 0px; max-width: 100%; width: auto;">
<tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;">
<td class="gutter" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; color: #666666; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px 20px 1px 1px; text-align: right; word-break: break-all;">1<br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">2</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">3</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">4</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">5</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">6</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">7</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">8</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">9</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">10</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">11</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">12</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">13</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">14</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">15</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">16</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">17</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">18</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">19</span><br style="box-sizing: border-box;" /></pre>
</td>
<td class="code" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;">List list = <span class="keyword" style="box-sizing: border-box; color: #8959a8;">new</span> LinkedList();</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">List spy = spy(list);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// optionally, you can stub out some methods:</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">when(spy.size()).thenReturn(<span class="number" style="box-sizing: border-box; color: #718c00;">100</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// using the spy calls *real* methods</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">spy.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">spy.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"two"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// prints "one" - the first element of a list</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">System.out.println(spy.get(<span class="number" style="box-sizing: border-box; color: #718c00;">0</span>));</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// size() method was stubbed - 100 is printed</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">System.out.println(spy.size());</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// optionally, you can verify</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(spy).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(spy).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"two"</span>);</span></pre>
</td>
</tr>
</tbody>
</table>
<p></p>
<p>As you can see from the code, the main difference between a Spy and a MockSettings is that the MockSettings for a Spy needs to be passed in a SpiedInstance.</p>
<p></p>
<p>The default Answer to a spy is CALLS_REAL_METHODS, which means that if a method is not stub, it performs its real behavior.</p>
<p>The default Answer to a mock is RETURNS_DEFAULTS. Methods that are not stub return a default value.</p>
<p></p>
<p>source：</p>
<p>https://www.vogella.com/tutorials/Mockito/article.html</p>
<p>https://howtodoinjava.com/mockito/junit-mockito-example/</p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>

<p class="syndicated-attribution"><em>From the blog <a href="https://haorusong.blogspot.com/search/label/CS%40Worcester">haorusong</a> by <a href="https://cs.worcester.edu/author/0/" title="Read other posts by Unknown">Unknown</a></em> and used with permission of the author. All other rights reserved by the author.</p>]]></content:encoded>
					
		
		<enclosure url="" length="0" type="" />

		<post-id xmlns="com-wordpress:feed-additions:1">39634</post-id>	</item>
		<item>
		<title>Detailed and basic usage of Mockito</title>
		<link>https://haorusong.blogspot.com/2021/04/detailed-and-basic-usage-of-mockito.html</link>
		
		<dc:creator><![CDATA[Unknown]]></dc:creator>
		<pubDate>Fri, 30 Apr 2021 14:38:00 +0000</pubDate>
				<category><![CDATA[#CS]]></category>
		<category><![CDATA[blog #6]]></category>
		<category><![CDATA[CS-443]]></category>
		<category><![CDATA[CS@Worcester]]></category>
		<guid isPermaLink="false">http://10.33.2.3/?guid=edb7057eb4b7f79269c2b31982134483</guid>

					<description><![CDATA[&#160;Mockito is a Mock framework for Java single-testing, but it can also be used with other single-testing frameworks in addition to JUnit. Mockito changes the behavior of a class or object, allowing us to focus more on testing the code logic without...]]></description>
										<content:encoded><![CDATA[<p>&nbsp;Mockito is a Mock framework for Java single-testing, but it can also be used with other single-testing frameworks in addition to JUnit. Mockito changes the behavior of a class or object, allowing us to focus more on testing the code logic without the effort of constructing the data.</p>
<p></p>
<p>The basic concept</p>
<p>Mocks can be of two types, Class and Partial，so Mockito is called spy. The behavior of changing methods on mock objects is called Stub.</p>
<p>A Mock process is called a Mock Session, and it records all the Stubbing. It consists of three steps:</p>
<p></p>
<p><span style="background-color: #f7f7f7; color: #4d4d4c; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 13px; white-space: pre;">+&#8212;&#8212;&#8212;-+      +&#8212;&#8212;+      +&#8212;&#8212;&#8211;+<br />
| Mock/Spy | ===&gt; | Stub | ===&gt; | Verify |<br />
+&#8212;&#8212;&#8212;-+      +&#8212;&#8212;+      +&#8212;&#8212;&#8211;+</span></p>
<p></p>
<p></p>
<p>Class Mock</p>
<p>A Class Mock changes the behavior of a Class so that the object it mocks completely loses its original behavior.</p>
<p>Method returns default values (null, false, 0, etc.) if it is not pegged.</p>
<p></p>
<p>The most basic usage is as follows:</p>
<table class="table" style="background-color: #f7f7f7; border-collapse: collapse; border-spacing: 0px; border: none; color: #4d4d4c; font-family: -apple-system, &quot;Helvetica Neue&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, STHeiti, &quot;Microsoft YaHei&quot;, &quot;Microsoft JhengHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;Source Han Sans CN&quot;, &quot;Noto Sans SC&quot;, &quot;Source Han Sans TC&quot;, &quot;Noto Sans CJK TC&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 13px; margin: 0px; max-width: 100%; width: auto;">
<tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;">
<td class="gutter" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; color: #666666; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px 20px 1px 1px; text-align: right; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;">1</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">2</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">3</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">4</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">5</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">6</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">7</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">8</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">9</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">10</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">11</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">12</span><br style="box-sizing: border-box;" /></pre>
</td>
<td class="code" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="keyword" style="box-sizing: border-box; color: #8959a8;">import</span> <span class="keyword" style="box-sizing: border-box; color: #8959a8;">static</span> org.mockito.Mockito.*;</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// use List.class to creat a mock subject --- mockedList</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">List mockedList = mock(List.class);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /></p>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">//operation of mockedList</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">mockedList.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">mockedList.clear();</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /></p>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">//validation</span></span></pre>
<p><span class="line" style="box-sizing: border-box; height: 20px;">verify(mockedList).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(mockedList).clear();</span></td>
</tr>
</tbody>
</table>
<p>Partial Mock（spy）</p>
<p>If we only want to change the behavior of an instance, we need to use spy:</p>
<table class="table" style="background-color: #f7f7f7; border-collapse: collapse; border-spacing: 0px; border: none; color: #4d4d4c; font-family: -apple-system, &quot;Helvetica Neue&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, STHeiti, &quot;Microsoft YaHei&quot;, &quot;Microsoft JhengHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;Source Han Sans CN&quot;, &quot;Noto Sans SC&quot;, &quot;Source Han Sans TC&quot;, &quot;Noto Sans CJK TC&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 13px; margin: 0px; max-width: 100%; width: auto;">
<tbody style="box-sizing: border-box;">
<tr style="box-sizing: border-box;">
<td class="gutter" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; color: #666666; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px 20px 1px 1px; text-align: right; word-break: break-all;">1<br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">2</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">3</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">4</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">5</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">6</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">7</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">8</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">9</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">10</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">11</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">12</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">13</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">14</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">15</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">16</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">17</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">18</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">19</span><br style="box-sizing: border-box;" /></pre>
</td>
<td class="code" style="border: none; box-sizing: border-box; line-height: 1.42857; padding: 0px; vertical-align: top;">
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 4px; border: none; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; line-height: 1.42857; margin-bottom: 0px; margin-top: 0px; overflow-wrap: break-word; overflow: auto; padding: 1px; word-break: break-all;"><span class="line" style="box-sizing: border-box; height: 20px;">List list = <span class="keyword" style="box-sizing: border-box; color: #8959a8;">new</span> LinkedList();</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">List spy = spy(list);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// optionally, you can stub out some methods:</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">when(spy.size()).thenReturn(<span class="number" style="box-sizing: border-box; color: #718c00;">100</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// using the spy calls *real* methods</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">spy.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">spy.add(<span class="string" style="box-sizing: border-box; color: #718c00;">"two"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// prints "one" - the first element of a list</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">System.out.println(spy.get(<span class="number" style="box-sizing: border-box; color: #718c00;">0</span>));</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// size() method was stubbed - 100 is printed</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">System.out.println(spy.size());</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;"><span class="comment" style="box-sizing: border-box; color: #8e908c; margin-top: 20px;">// optionally, you can verify</span></span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(spy).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"one"</span>);</span><br style="box-sizing: border-box;" /><span class="line" style="box-sizing: border-box; height: 20px;">verify(spy).add(<span class="string" style="box-sizing: border-box; color: #718c00;">"two"</span>);</span></pre>
</td>
</tr>
</tbody>
</table>
<p></p>
<p>As you can see from the code, the main difference between a Spy and a MockSettings is that the MockSettings for a Spy needs to be passed in a SpiedInstance.</p>
<p></p>
<p>The default Answer to a spy is CALLS_REAL_METHODS, which means that if a method is not stub, it performs its real behavior.</p>
<p>The default Answer to a mock is RETURNS_DEFAULTS. Methods that are not stub return a default value.</p>
<p></p>
<p>source：</p>
<p>https://www.vogella.com/tutorials/Mockito/article.html</p>
<p>https://howtodoinjava.com/mockito/junit-mockito-example/</p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>

<p class="syndicated-attribution"><em>From the blog <a href="https://haorusong.blogspot.com/search/label/CS%40Worcester">haorusong</a> by <a href="https://cs.worcester.edu/author/0/" title="Read other posts by Unknown">Unknown</a></em> and used with permission of the author. All other rights reserved by the author.</p>]]></content:encoded>
					
		
		<enclosure url="" length="0" type="" />

		<post-id xmlns="com-wordpress:feed-additions:1">39705</post-id>	</item>
		<item>
		<title>Breakable Toys</title>
		<link>https://haorusong.blogspot.com/2021/04/breakable-toys.html</link>
		
		<dc:creator><![CDATA[Unknown]]></dc:creator>
		<pubDate>Mon, 19 Apr 2021 03:38:00 +0000</pubDate>
				<category><![CDATA[#CS]]></category>
		<category><![CDATA[blog #6]]></category>
		<category><![CDATA[CS-448]]></category>
		<category><![CDATA[CS@Worcester]]></category>
		<category><![CDATA[Week 12]]></category>
		<guid isPermaLink="false">http://cs.worcester.edu/?guid=b3e4a1f7df340096543eeb507933c2eb</guid>

					<description><![CDATA[&#160;We can all benefit from writing random "toy" programs to push ourselves to the limit by setting artificial limits. -- Donald Knuth, The Art of Computer ProgrammingIf you can learn as much from failure as you can from success, you need a relativel...]]></description>
										<content:encoded><![CDATA[<p>&nbsp;We can all benefit from writing random &#8220;toy&#8221; programs to push ourselves to the limit by setting artificial limits. &#8212; Donald Knuth, The Art of Computer Programming</p>
<p></p>
<p>If you can learn as much from failure as you can from success, you need a relatively private space to look for the loss. In balling acrobatics, a performer who tosses three balls will never progress if he has never tossed five. Those who spend hours picking dropped balls until their back hurts eventually get good at it. The same lesson applies to software, where software workers often need to step out of their comfort zone to try something they&#8217;re not good at it. It is by taking bold chances again and again that you improve your skills, that you learn and grow from failure after failure. Just as a three-ball-tossing performer does not throw five balls in a formal performance, software developers often make mistakes in new areas. Software developers also need a safe place to make mistakes. It&#8217;s terrible not to have such a safe place to make mistakes. The fear and unwillingness of software developers to make mistakes make them stuck in a rut, unable to learn from failure, and thus makes the entire software development industry staid and stagnant.</p>
<p></p>
<p>Other examples of the &#8220;crunchy toy&#8221; pattern include games like Tetris and Tic-Tac-Toe. One senior I know has a habit of using every new language he learns to create a game, blog software, and IRC client. The nature of the problem is to build a toy that contains learning something new and provides an opportunity to let you in a special environment to deepen the understanding of hand tools. The environment is not only safe because you are the only or most influential users, and, even to the most powerful commercial products, you still have room to serve you as a user&#8217;s needs better.</p>
<p></p>
<p>You still have to remember that they&#8217;re just toys, and that&#8217;s why they&#8217;re supposed to be fun. If they&#8217;re not interesting, then when the initial excitement is over, they&#8217;ll just become a dusty relic, and you&#8217;ll be able to focus your energy on what you enjoy building.</p>

<p class="syndicated-attribution"><em>From the blog <a href="https://haorusong.blogspot.com/search/label/CS%40Worcester">haorusong</a> by <a href="https://cs.worcester.edu/author/0/" title="Read other posts by Unknown">Unknown</a></em> and used with permission of the author. All other rights reserved by the author.</p>]]></content:encoded>
					
		
		<enclosure url="" length="0" type="" />

		<post-id xmlns="com-wordpress:feed-additions:1">39640</post-id>	</item>
		<item>
		<title>Breakable Toys</title>
		<link>https://haorusong.blogspot.com/2021/04/breakable-toys.html</link>
		
		<dc:creator><![CDATA[Unknown]]></dc:creator>
		<pubDate>Mon, 19 Apr 2021 03:38:00 +0000</pubDate>
				<category><![CDATA[#CS]]></category>
		<category><![CDATA[blog #6]]></category>
		<category><![CDATA[CS-448]]></category>
		<category><![CDATA[CS@Worcester]]></category>
		<category><![CDATA[Week 12]]></category>
		<guid isPermaLink="false">http://10.33.2.3/?guid=b3e4a1f7df340096543eeb507933c2eb</guid>

					<description><![CDATA[&#160;We can all benefit from writing random "toy" programs to push ourselves to the limit by setting artificial limits. -- Donald Knuth, The Art of Computer ProgrammingIf you can learn as much from failure as you can from success, you need a relativel...]]></description>
										<content:encoded><![CDATA[<p>&nbsp;We can all benefit from writing random &#8220;toy&#8221; programs to push ourselves to the limit by setting artificial limits. &#8212; Donald Knuth, The Art of Computer Programming</p>
<p></p>
<p>If you can learn as much from failure as you can from success, you need a relatively private space to look for the loss. In balling acrobatics, a performer who tosses three balls will never progress if he has never tossed five. Those who spend hours picking dropped balls until their back hurts eventually get good at it. The same lesson applies to software, where software workers often need to step out of their comfort zone to try something they&#8217;re not good at it. It is by taking bold chances again and again that you improve your skills, that you learn and grow from failure after failure. Just as a three-ball-tossing performer does not throw five balls in a formal performance, software developers often make mistakes in new areas. Software developers also need a safe place to make mistakes. It&#8217;s terrible not to have such a safe place to make mistakes. The fear and unwillingness of software developers to make mistakes make them stuck in a rut, unable to learn from failure, and thus makes the entire software development industry staid and stagnant.</p>
<p></p>
<p>Other examples of the &#8220;crunchy toy&#8221; pattern include games like Tetris and Tic-Tac-Toe. One senior I know has a habit of using every new language he learns to create a game, blog software, and IRC client. The nature of the problem is to build a toy that contains learning something new and provides an opportunity to let you in a special environment to deepen the understanding of hand tools. The environment is not only safe because you are the only or most influential users, and, even to the most powerful commercial products, you still have room to serve you as a user&#8217;s needs better.</p>
<p></p>
<p>You still have to remember that they&#8217;re just toys, and that&#8217;s why they&#8217;re supposed to be fun. If they&#8217;re not interesting, then when the initial excitement is over, they&#8217;ll just become a dusty relic, and you&#8217;ll be able to focus your energy on what you enjoy building.</p>

<p class="syndicated-attribution"><em>From the blog <a href="https://haorusong.blogspot.com/search/label/CS%40Worcester">haorusong</a> by <a href="https://cs.worcester.edu/author/0/" title="Read other posts by Unknown">Unknown</a></em> and used with permission of the author. All other rights reserved by the author.</p>]]></content:encoded>
					
		
		<enclosure url="" length="0" type="" />

		<post-id xmlns="com-wordpress:feed-additions:1">39711</post-id>	</item>
	</channel>
</rss>
