﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-thinking-随笔分类-Unit Test</title><link>http://www.blogjava.net/alinglau36/category/44793.html</link><description>one platform thousands thinking</description><language>zh-cn</language><lastBuildDate>Thu, 02 Nov 2017 21:44:44 GMT</lastBuildDate><pubDate>Thu, 02 Nov 2017 21:44:44 GMT</pubDate><ttl>60</ttl><item><title>java模拟并发操作进行压力测试</title><link>http://www.blogjava.net/alinglau36/archive/2010/05/28/322118.html</link><dc:creator>lau</dc:creator><author>lau</author><pubDate>Fri, 28 May 2010 03:06:00 GMT</pubDate><guid>http://www.blogjava.net/alinglau36/archive/2010/05/28/322118.html</guid><wfw:comment>http://www.blogjava.net/alinglau36/comments/322118.html</wfw:comment><comments>http://www.blogjava.net/alinglau36/archive/2010/05/28/322118.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/alinglau36/comments/commentRss/322118.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alinglau36/services/trackbacks/322118.html</trackback:ping><description><![CDATA[<a href="http://www.qqread.com/java/2010/01/c488170.html">http://www.qqread.com/java/2010/01/c488170.html</a><br />
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.BufferedReader;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.File;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.FileInputStream;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.InputStreamReader;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.io.PrintWriter;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.net.HttpURLConnection;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.net.URL;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.util.HashMap;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.util.Map;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.util.concurrent.ExecutorService;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.util.concurrent.Executors;<br />
<br />
</span><span style="color: #0000ff;">import</span><span style="color: #000000;">&nbsp;java.util.concurrent.Semaphore;<br />
<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;ConcurrentTest&nbsp;{<br />
<br />
</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;thread_num&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">200</span><span style="color: #000000;">;<br />
<br />
</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;client_num&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">460</span><span style="color: #000000;">;<br />
<br />
</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;Map&nbsp;keywordMap&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;HashMap();<br />
<br />
</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;{<br />
<br />
</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
<br />
InputStreamReader&nbsp;isr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;InputStreamReader(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;FileInputStream(<br />
<br />
</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;File(</span><span style="color: #000000;">"</span><span style="color: #000000;">clicks.txt</span><span style="color: #000000;">"</span><span style="color: #000000;">)),&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">GBK</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
<br />
BufferedReader&nbsp;buffer&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;BufferedReader(isr);<br />
<br />
String&nbsp;line&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">""</span><span style="color: #000000;">;<br />
<br />
</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;((line&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;buffer.readLine())&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
<br />
keywordMap.put(line.substring(</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;line.lastIndexOf(</span><span style="color: #000000;">"</span><span style="color: #000000;">:</span><span style="color: #000000;">"</span><span style="color: #000000;">)),&nbsp;</span><span style="color: #000000;">""</span><span style="color: #000000;">);<br />
<br />
}<br />
<br />
}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
<br />
e.printStackTrace();<br />
<br />
}<br />
<br />
}<br />
<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
<br />
</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;size&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;keywordMap.size();<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;TODO&nbsp;Auto-generated&nbsp;method&nbsp;stub</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
ExecutorService&nbsp;exec&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Executors.newCachedThreadPool();<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;50个线程可以同时访问</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;Semaphore&nbsp;semp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Semaphore(thread_num);<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;模拟2000个客户端访问</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;index&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;index&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;client_num;&nbsp;index</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br />
<br />
</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;NO&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;index;<br />
<br />
Runnable&nbsp;run&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Runnable()&nbsp;{<br />
<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;run()&nbsp;{<br />
<br />
</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;{<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;获取许可</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
semp.acquire();<br />
<br />
System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Thread:</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;NO);<br />
<br />
String&nbsp;host&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">http://10.99.23.42:7001/KMQueryCenter/query.do?</span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
<br />
String&nbsp;para&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">method=getQueryResult&amp;pageNum=1&amp;pageSize=5&amp;</span><span style="color: #000000;">"</span><span style="color: #000000;"><br />
<br />
</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">queryKeyWord=</span><span style="color: #000000;">"</span><span style="color: #000000;"><br />
<br />
</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;getRandomSearchKey(NO)<br />
<br />
</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&amp;questionID=-1&amp;questionIdPath=-1&amp;searchType=1</span><span style="color: #000000;">"</span><span style="color: #000000;"><br />
<br />
</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&amp;proLine=&amp;proSeries=&amp;proType=</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;NO;<br />
<br />
System.out.println(host&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;para);<br />
<br />
URL&nbsp;url&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;URL(host);</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;此处填写供测试的url</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
HttpURLConnection&nbsp;connection&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(HttpURLConnection)&nbsp;url<br />
<br />
.openConnection();<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;connection.setRequestMethod("POST");<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;connection.setRequestProperty("Proxy-Connection",<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"Keep-Alive");</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
connection.setDoOutput(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);<br />
<br />
connection.setDoInput(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);<br />
<br />
PrintWriter&nbsp;out&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;PrintWriter(connection<br />
<br />
.getOutputStream());<br />
<br />
out.print(para);<br />
<br />
out.flush();<br />
<br />
out.close();<br />
<br />
BufferedReader&nbsp;in&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;BufferedReader(<br />
<br />
</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;InputStreamReader(connection<br />
<br />
.getInputStream()));<br />
<br />
String&nbsp;line&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">""</span><span style="color: #000000;">;<br />
<br />
String&nbsp;result&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">""</span><span style="color: #000000;">;<br />
<br />
</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;((line&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;in.readLine())&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">)&nbsp;{<br />
<br />
result&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;line;<br />
<br />
}<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;System.out.println(result);<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;Thread.sleep((long)&nbsp;(Math.random())&nbsp;*&nbsp;1000);<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;释放</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">第：</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;NO&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;个</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
<br />
semp.release();<br />
<br />
}&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;(Exception&nbsp;e)&nbsp;{<br />
<br />
e.printStackTrace();<br />
<br />
}<br />
<br />
}<br />
<br />
};<br />
<br />
exec.execute(run);<br />
<br />
}<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;退出线程池</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
exec.shutdown();<br />
<br />
}<br />
<br />
</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;String&nbsp;getRandomSearchKey(</span><span style="color: #0000ff;">final</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;no)&nbsp;{<br />
<br />
String&nbsp;ret&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">""</span><span style="color: #000000;">;<br />
<br />
</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;size&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;keywordMap.size();<br />
<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;int&nbsp;wanna&nbsp;=&nbsp;(int)&nbsp;(Math.random())&nbsp;*&nbsp;(size&nbsp;-&nbsp;1);</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
ret&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(keywordMap.entrySet().toArray())[no].toString();<br />
<br />
ret&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ret.substring(</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;ret.lastIndexOf(</span><span style="color: #000000;">"</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">));<br />
<br />
System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">\t</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;ret);<br />
<br />
</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;ret;<br />
<br />
}<br />
<br />
}</span></div>
<br /><strong>如果本文对您有帮助并且要鼓励我的话，请扫描如下二维码支持本人的劳动成果，多谢了！</strong><br /><br /><img src="http://www.blogjava.net/images/blogjava_net/alinglau36/wcpay.jpg" width="500" height="685" alt="" /><img src ="http://www.blogjava.net/alinglau36/aggbug/322118.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alinglau36/" target="_blank">lau</a> 2010-05-28 11:06 <a href="http://www.blogjava.net/alinglau36/archive/2010/05/28/322118.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EasyMock最佳实践</title><link>http://www.blogjava.net/alinglau36/archive/2010/05/21/321523.html</link><dc:creator>lau</dc:creator><author>lau</author><pubDate>Fri, 21 May 2010 02:56:00 GMT</pubDate><guid>http://www.blogjava.net/alinglau36/archive/2010/05/21/321523.html</guid><wfw:comment>http://www.blogjava.net/alinglau36/comments/321523.html</wfw:comment><comments>http://www.blogjava.net/alinglau36/archive/2010/05/21/321523.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alinglau36/comments/commentRss/321523.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alinglau36/services/trackbacks/321523.html</trackback:ping><description><![CDATA[Source: <a href="http://macrochen.javaeye.com/blog/298032">http://macrochen.javaeye.com/blog/298032</a><br />
<img src ="http://www.blogjava.net/alinglau36/aggbug/321523.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alinglau36/" target="_blank">lau</a> 2010-05-21 10:56 <a href="http://www.blogjava.net/alinglau36/archive/2010/05/21/321523.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Fun with EasyMock</title><link>http://www.blogjava.net/alinglau36/archive/2010/04/21/319014.html</link><dc:creator>lau</dc:creator><author>lau</author><pubDate>Wed, 21 Apr 2010 10:23:00 GMT</pubDate><guid>http://www.blogjava.net/alinglau36/archive/2010/04/21/319014.html</guid><wfw:comment>http://www.blogjava.net/alinglau36/comments/319014.html</wfw:comment><comments>http://www.blogjava.net/alinglau36/archive/2010/04/21/319014.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alinglau36/comments/commentRss/319014.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alinglau36/services/trackbacks/319014.html</trackback:ping><description><![CDATA[<div>
<h1 id="post-43"><a href="http://burtbeckwith.com/blog/?p=43" rel="bookmark" title="Permanent Link: Fun with EasyMock">Fun with EasyMock</a></h1>
<div>
<p>I'm a big fan of <a target="_blank" href="http://en.wikipedia.org/wiki/Test-driven_development">Test-driven development<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a> and have been using <a target="_blank" href="http://junit.org/">JUnit<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a>
for 6 or 7 years (since around v3.4 or v3.5). I've written lots of mock
objects (from simplistic dummy concrete classes that implement an
entire interface, to <a target="_blank" href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Proxy.html">Proxy<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a>-based
mocks, to a pretty decent mock JMS library) and played with various
mock libraries, but the one I'm sticking with and using almost
exclusively is <a target="_blank" href="http://www.easymock.org/">EasyMock<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a>.</p>
<p>Reinventing a wheel is great for learning, and I've done it a lot.
But perhaps in a sign that I'm getting older, I'm more concerned with
getting stuff done, so now I tend to lean towards existing solutions
when good ones exist. I searched for what people tend to use for mock
objects and <a target="_blank" href="http://www.jmock.org/%27">jMock<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a>
and EasyMock are popular. I prefer the method-based approach of
EasyMock to the string-based approach of jMock, since code changes will
cause the tests to break immediately, and the mocks will be refactored
along with the rest of the code if I use the IDE's refactoring features.</p>
<p>The general pattern with creating mocks for a tier of your
application is that you're trusting that that tier is tested and works
correctly, so it's appropriate to have mocks return hard-coded values.
We're testing that the caller of the mocked interface does the right
thing with correct (or incorrect) inputs, so mocks help us to focus on
what's being tested in isolation.</p>
<p>I'd always found it difficult to unit test <a target="_blank" href="http://www.springframework.org/">Spring<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a> <a target="_blank" href="http://www.springframework.org/docs/reference/mvc.html">MVC<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a> controllers, since it seems like they need a running container, and I don't want to deal with the hassle of setting up <a target="_blank" href="http://jakarta.apache.org/cactus/">Cactus<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a>.
Plus it seems somewhat useless to test controllers, since they
shouldn't be doing much that isn't tested in proper automated
functional tests. If they do too much business logic, they should be
refactored to push that work out to the services layer, right?</p>
<p>The reality is that we do too much business logic in controllers,
and usually it's lazyness but often it's being pragmatic. We also tend
to often do even more "illegal" activities in controllers, such as
writing directly to the response rather than delegating to a <code>View</code>. This is usually the case for XML and JSON generated for Ajax responses. So until these get refactored, they need to be tested.</p>
<p>It's helpful to have partially-functioning servlet container classes such as <code>HttpServletRequest</code> and <code>HttpServletResponse</code>,
and the Spring Mock library (spring-mock.jar) is great for that. But
for creating mocks for DAOs, services, and other interface-based
dependency-injected resources, EasyMock greatly simplifies things. And
with their user-contributed <a target="_blank" href="http://www.easymock.org/EasyMock2_0_ClassExtension_Documentation.html">Class Extension<img src="http://burtbeckwith.com/blog/images/pop.gif" alt="" /></a>, you can even mock out concrete classes.</p>
<p>The syntax takes a while to get used to. Basically the flow that I tend to use is:</p>
<ol>
    <li>Create a mock</li>
    <li>call <code>expect(mock.[method call]).andReturn([result])</code> for each expected call</li>
    <li>call <code>mock.[method call]</code>, then <code>EasyMock.expectLastCall()</code> for each expected void call</li>
    <li>call <code>replay(mock)</code> to switch from "record" mode to "playback" mode</li>
    <li>inject the mock as needed</li>
    <li>call the test method</li>
    <li>call <code>verify(mock)</code> to assure that all expected calls happened</li>
</ol>
<p>As simple as that may sound, it can look very weird at first. Say I have an interface:</p>
<div><span id="ljava-1"><a href="http://burtbeckwith.com/blog/?p=43#" onclick="javascript:showPlainTxt('java-1'); return false;">PLAIN TEXT</a></span></div>
<div>JAVA:
<div id="java-1">
<div>
<ol>
    <li style="font-family: 'Courier New',Courier,monospace; font-weight: normal; font-style: normal; color: rgb(58, 106, 139);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;"><span style="color: rgb(0, 0, 153); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 153); font-weight: bold;">interface</span> Thing <span style="color: rgb(0, 0, 0);">{</span></div>
    </li>
    <li style="font-weight: bold; color: rgb(38, 83, 106);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: rgb(0, 0, 255);">void</span> doSomething<span style="color: rgb(0, 0, 0);">(</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: rgb(0, 0, 0);">String</span></a> parameter<span style="color: rgb(0, 0, 0);">)</span>;</div>
    </li>
    <li style="font-family: 'Courier New',Courier,monospace; font-weight: normal; font-style: normal; color: rgb(58, 106, 139);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: rgb(0, 0, 255);">int</span> doSomethingElse<span style="color: rgb(0, 0, 0);">(</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: rgb(0, 0, 0);">String</span></a> parameter, <span style="color: rgb(0, 0, 255);">int</span> otherParameter<span style="color: rgb(0, 0, 0);">)</span>;</div>
    </li>
    <li style="font-weight: bold; color: rgb(38, 83, 106);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;"><span style="color: rgb(0, 0, 0);">}</span> </div>
    </li>
</ol>
</div>
</div>
</div>
<p>I can create a mock for it via:</p>
<div><span id="ljava-2"><a href="http://burtbeckwith.com/blog/?p=43#" onclick="javascript:showPlainTxt('java-2'); return false;">PLAIN TEXT</a></span></div>
<div>JAVA:
<div id="java-2">
<div>
<ol>
    <li style="font-family: 'Courier New',Courier,monospace; font-weight: normal; font-style: normal; color: rgb(58, 106, 139);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">Thing thing = EasyMock.<span style="color: rgb(0, 0, 0);">createMock</span><span style="color: rgb(0, 0, 0);">(</span>Thing.<span style="color: rgb(0, 0, 153); font-weight: bold;">class</span><span style="color: rgb(0, 0, 0);">)</span>; </div>
    </li>
</ol>
</div>
</div>
</div>
<p>Then I can register expected calls via</p>
<div><span id="ljava-3"><a href="http://burtbeckwith.com/blog/?p=43#" onclick="javascript:showPlainTxt('java-3'); return false;">PLAIN TEXT</a></span></div>
<div>JAVA:
<div id="java-3">
<div>
<ol>
    <li style="font-family: 'Courier New',Courier,monospace; font-weight: normal; font-style: normal; color: rgb(58, 106, 139);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">EasyMock.<span style="color: rgb(0, 0, 0);">expect</span><span style="color: rgb(0, 0, 0);">(</span>thing.<span style="color: rgb(0, 0, 0);">doSomethingElse</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">"woot"</span>, <span style="color: rgb(128, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">)</span>.<span style="color: rgb(0, 0, 0);">andReturn</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0);">123</span><span style="color: rgb(0, 0, 0);">)</span>;</div>
    </li>
    <li style="font-weight: bold; color: rgb(38, 83, 106);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">EasyMock.<span style="color: rgb(0, 0, 0);">expect</span><span style="color: rgb(0, 0, 0);">(</span>thing.<span style="color: rgb(0, 0, 0);">doSomethingElse</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">"fubar"</span>, <span style="color: rgb(128, 0, 0);">45</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">)</span>.<span style="color: rgb(0, 0, 0);">andReturn</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0);">321</span><span style="color: rgb(0, 0, 0);">)</span>;</div>
    </li>
    <li style="font-family: 'Courier New',Courier,monospace; font-weight: normal; font-style: normal; color: rgb(58, 106, 139);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">&nbsp;</div>
    </li>
    <li style="font-weight: bold; color: rgb(38, 83, 106);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">thing.<span style="color: rgb(0, 0, 0);">doSomething</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">"p"</span><span style="color: rgb(0, 0, 0);">)</span>;</div>
    </li>
    <li style="font-family: 'Courier New',Courier,monospace; font-weight: normal; font-style: normal; color: rgb(58, 106, 139);">
    <div style="font-family: 'Courier New',Courier,monospace; font-weight: normal;">EasyMock.<span style="color: rgb(0, 0, 0);">expectLastCall</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">)</span>; </div>
    </li>
</ol>
</div>
</div>
</div>
<p>This says that in my calling code, when the client calls <code>thing.doSomethingElse("woot", 5)</code> to return 123, when the client calls <code>thing.doSomethingElse("fubar", 45)</code> to return 321, and to expect a call to <code>thing.doSomething("p")</code>.</p>
<p>I can then inject this mock into the class being tested in place of
the real one, trusting that it will return known results. I can then
concentrate on assuring that the tested class does the right thing.</p>
<p>Thanks to EasyMock, my productivity for testing services and controllers is way up - and my code coverage percentages are too.</p>
<p alt="">
<small>
This entry was posted on Monday, August 13th, 2007 at 2:43am
and is filed under <a href="http://burtbeckwith.com/blog/?cat=15" title="View all posts in easymock" rel="category">easymock</a>,  <a href="http://burtbeckwith.com/blog/?cat=2" title="View all posts in java" rel="category">java</a>,  <a href="http://burtbeckwith.com/blog/?cat=14" title="View all posts in junit" rel="category">junit</a>.
You can follow any responses to this entry through the <a href="http://burtbeckwith.com/blog/?feed=rss2&amp;p=43">RSS 2.0</a> feed.
You can skip to the end and leave a response. Pinging is currently not allowed. <br />
</small></p>
<p alt=""><br />
</p>
<p alt=""><small>Source from </small>http://burtbeckwith.com/blog/?p=43
</p>
</div>
</div>
<img src ="http://www.blogjava.net/alinglau36/aggbug/319014.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alinglau36/" target="_blank">lau</a> 2010-04-21 18:23 <a href="http://www.blogjava.net/alinglau36/archive/2010/04/21/319014.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Mock void method</title><link>http://www.blogjava.net/alinglau36/archive/2010/04/21/319013.html</link><dc:creator>lau</dc:creator><author>lau</author><pubDate>Wed, 21 Apr 2010 10:23:00 GMT</pubDate><guid>http://www.blogjava.net/alinglau36/archive/2010/04/21/319013.html</guid><wfw:comment>http://www.blogjava.net/alinglau36/comments/319013.html</wfw:comment><comments>http://www.blogjava.net/alinglau36/archive/2010/04/21/319013.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alinglau36/comments/commentRss/319013.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alinglau36/services/trackbacks/319013.html</trackback:ping><description><![CDATA[<div>
<p>I have a method that returns void in a class that is a dependency of the class I want to test.</p>
<p>This class is huge and I'm only using this single method from it.
I need to replace the implementation of this method for the test as I
want it to do something different and I need to be able to access the
parameters this method receives.</p>
<p>I cannot find a way of doing this in EasyMock. I think I know how to
do it with Mockito by using doAnswer but I don't want to add another
library unless absolutely necessary.</p>
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000;">If&nbsp;I&nbsp;understand&nbsp;what&nbsp;you&nbsp;want&nbsp;to&nbsp;</span><span style="color: #0000ff;">do</span><span style="color: #000000;">&nbsp;correctly,&nbsp;you&nbsp;should&nbsp;be&nbsp;able&nbsp;to&nbsp;use&nbsp;andAnswer():<br />
<br />
mockObject.someMethod(eq(param1),&nbsp;eq(param2);<br />
<br />
expectLastCall().andAnswer(newIAnswer(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;publicObject&nbsp;answer(){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">supply&nbsp;your&nbsp;mock&nbsp;implementation&nbsp;here<img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SomeClass&nbsp;arg1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">(SomeClass)&nbsp;getCurrentArguments()[</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnotherClass&nbsp;arg2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">(AnotherClass)&nbsp;getCurrentArguments()[</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg1.doSomething(blah);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">return&nbsp;the&nbsp;value&nbsp;to&nbsp;be&nbsp;returned&nbsp;by&nbsp;the&nbsp;method&nbsp;(null&nbsp;for&nbsp;void)</span><span style="color: #000000;"><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnnull;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
});</span></div>
</div>
<img src ="http://www.blogjava.net/alinglau36/aggbug/319013.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alinglau36/" target="_blank">lau</a> 2010-04-21 18:23 <a href="http://www.blogjava.net/alinglau36/archive/2010/04/21/319013.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Mocking Static Calls</title><link>http://www.blogjava.net/alinglau36/archive/2010/02/25/313846.html</link><dc:creator>lau</dc:creator><author>lau</author><pubDate>Thu, 25 Feb 2010 01:12:00 GMT</pubDate><guid>http://www.blogjava.net/alinglau36/archive/2010/02/25/313846.html</guid><wfw:comment>http://www.blogjava.net/alinglau36/comments/313846.html</wfw:comment><comments>http://www.blogjava.net/alinglau36/archive/2010/02/25/313846.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/alinglau36/comments/commentRss/313846.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/alinglau36/services/trackbacks/313846.html</trackback:ping><description><![CDATA[<p>How can you test methods that contain static method calls? This is a
question we're facing at the moment while working on an application of
which parts have been written by another development team. In order to
gain insight into functionality and quality of the codebase, we are
writing JUnit tests. But a lot of the code is dependent on either the
JSF FacesContext or the Spring ApplicationContext being available.<br />
</p>
<p>It is however impossible to mock static method calls like <em>FacesContext.getCurrentInstance()</em>.
So how do we go around testing these calls out of container? We've
thought up three different approaches, but each has its pros and cons.
Let us first take a look at these three approaches...</p>
<p><strong>1. Extract method</strong><br />
Introduce a method in each class that looks like the following:</p>
<pre>&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">protected</span> FacesContext getFacesContext<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">return</span> FacesContext.<span style="color: rgb(0, 102, 0);">getCurrentInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;</pre>
<p>This can be easily mocked out in your test by creating the class with an overridden method, as follows:</p>
<pre>&nbsp;<br />
FacesContext mockContext = EasyMock.<span style="color: rgb(0, 102, 0);">createMock</span><span style="color: rgb(102, 204, 102);">(</span>FacesContext.<span style="color: rgb(0, 0, 0); font-weight: bold;">class</span><span style="color: rgb(102, 204, 102);">)</span>;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> MyObjectUnderTest<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">protected</span> FacesContext getFacesContext<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">return</span> mockContext;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;</pre>
<p><strong>Pros</strong></p>
<ul>
    <li>Easy to refactor using extract method</li>
</ul>
<p><strong>Cons</strong></p>
<ul>
    <li>Lots of code duplication</li>
</ul>
<p><strong>2. Static Helper</strong><br />
Our second option is to introduce a static helper class which has the following implementation:</p>
<pre>&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> FacesContextHelper <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">private</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">static</span> FacesContext context;<br />
&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">static</span> FacesContext getCurrentFacesContext<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">return</span> context != <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span> ? context : FacesContext.<span style="color: rgb(0, 102, 0);">getCurrentInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">static</span> <span style="color: rgb(153, 51, 51);">void</span> setFacesContext<span style="color: rgb(102, 204, 102);">(</span>FacesContext facesContext<span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
context = facesContext;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">static</span> <span style="color: rgb(153, 51, 51);">void</span> reset<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
context = <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span>;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;</pre>
<p>Now in a testclass we can just write down the following line to mock out the FacesContext:</p>
<pre>&nbsp;<br />
FacesContextHelper.<span style="color: rgb(0, 102, 0);">setFacesContext</span><span style="color: rgb(102, 204, 102);">(</span>EasyMock.<span style="color: rgb(0, 102, 0);">createMock</span><span style="color: rgb(102, 204, 102);">(</span>FacesContext.<span style="color: rgb(0, 0, 0); font-weight: bold;">class</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>;<br />
&nbsp;</pre>
<p><strong>Pros</strong></p>
<ul>
    <li>No real painful code-duplication</li>
    <li>Easy to do search-replace to convert all calls to
    FacesContext.getCurrentInstance() to
    FacesContextHelper.getCurrentFacesContext().</li>
</ul>
<p><strong>Cons</strong></p>
<ul>
    <li>Never forget to call reset() in the teardown of your test to prevent another test from picking up your mock.</li>
    <li>Doesn't feel right to write static setter methods in your code <em>just</em> to enable testing it. Not an OO approach?</li>
</ul>
<p><strong>3. OO Helper class</strong><br />
Our third and last option is to introduce an interface and implementation of the FacesContextHelper:</p>
<pre>&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">interface</span> FacesContextHelper <span style="color: rgb(102, 204, 102);">{</span><br />
FacesContext getCurrentFacesContext<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> FacesContextHelperImpl <span style="color: rgb(0, 0, 0); font-weight: bold;">implements</span> FacesContextHelper <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> FacesContext getCurrentFacesContext<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">return</span> FacesContext.<span style="color: rgb(0, 102, 0);">getCurrentInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;</pre>
<p>We now need to introduce an instance of the FacesContextHelperImpl
to each class. For this example, we will use a package protected
variable in each class. It is of course also possible to use a setter
method. For our test cases we now need to introduce a new
FacesContextHelper:</p>
<pre>&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> MockFacesContextHelper <span style="color: rgb(0, 0, 0); font-weight: bold;">implements</span> FacesContextHelper <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">private</span> FacesContext mockContext;<br />
&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> MockFacesContextHelper<span style="color: rgb(102, 204, 102);">(</span>FacesContext mockContext<span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">this</span>.<span style="color: rgb(0, 102, 0);">mockContext</span> = mockContext;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;<br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">public</span> FacesContext getCurrentFacesContext<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span><br />
<span style="color: rgb(0, 0, 0); font-weight: bold;">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">this</span>.<span style="color: rgb(0, 102, 0);">mockContext</span>;<br />
<span style="color: rgb(102, 204, 102);">}</span><br />
<span style="color: rgb(102, 204, 102);">}</span><br />
&nbsp;</pre>
<p>In our test cases we can now easily mock out the FacesContext again by setting the package protected field:</p>
<pre>&nbsp;<br />
someClazz.<span style="color: rgb(0, 102, 0);">facesContextHelper</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> MockFacesContextHelper<span style="color: rgb(102, 204, 102);">(</span>EasyMock.<span style="color: rgb(0, 102, 0);">createMock</span><span style="color: rgb(102, 204, 102);">(</span>FacesContext.<span style="color: rgb(0, 0, 0); font-weight: bold;">class</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>;<br />
&nbsp;</pre>
<p><strong>Pros</strong></p>
<ul>
    <li>Feels more natural and OO than the static helper solution</li>
</ul>
<p><strong>Cons</strong></p>
<ul>
    <li>Need to introduce a new field to each and every class which needs the FacesContext mocked out.</li>
</ul>
<p>That's it. I myself am still doubting as to which method is the
lesser evil, not one feels absolutely right. What do you think, which
method do you consider better? Did I overlook another option perhaps?</p>
<img src ="http://www.blogjava.net/alinglau36/aggbug/313846.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/alinglau36/" target="_blank">lau</a> 2010-02-25 09:12 <a href="http://www.blogjava.net/alinglau36/archive/2010/02/25/313846.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>