www.lyjust.netEasyMockJUnit集成

Git ww9992019com 发表于 1 周前

www.lyjust.net开户请拨18288069766张曼玉回忆张国荣天津红桥区dDOYjJ<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="5819">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

在本章中,我们将集成JUnit和EasyMock在一起。对于JUnit,请参阅<a href="http://www.yiibai.com/junit/">JUnit教程</a>。我们使用计算器服务的例子。目的是创建一个数学应用,它使用CalculatorService做加,减,除运算操作。我们将使用EasyMock来模拟虚拟实现CalculatorService。此外,使用注解广泛展示注解支持JUnit和EasyMock。</p>

<p>

以下是所采取的步骤。</p>

<p>

创建一个接口CalculatorService,其目的是提供各种计算相关的功能。</p>

CalculatorService.java

<pre class="prettyprint prettyprinted" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb>

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">interface</span><span class="pln"> </span><span class="typ">CalculatorService</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> add</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> subtract</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> multiply</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> divide</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span></pre>

<p>

创建一个Java类来表示MathApplication.</p>

MathApplication.java

<pre class="prettyprint prettyprinted" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb>

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MathApplication</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">private</span><span class="pln"> </span><span class="typ">CalculatorService</span><span class="pln"> calcService</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setCalculatorService</span><span class="pun">(</span><span class="typ">CalculatorService</span><span class="pln"> calcService</span><span class="pun">){</span><span class="pln">

  </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">calcService </span><span class="pun">=</span><span class="pln"> calcService</span><span class="pun">;</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> add</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">){</span><span class="pln">

  </span><span class="kwd">return</span><span class="pln"> calcService</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="pln">input1</span><span class="pun">,</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> subtract</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">){</span><span class="pln">

  </span><span class="kwd">return</span><span class="pln"> calcService</span><span class="pun">.</span><span class="pln">subtract</span><span class="pun">(</span><span class="pln">input1</span><span class="pun">,</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> multiply</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">){</span><span class="pln">

  </span><span class="kwd">return</span><span class="pln"> calcService</span><span class="pun">.</span><span class="pln">multiply</span><span class="pun">(</span><span class="pln">input1</span><span class="pun">,</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> divide</span><span class="pun">(</span><span class="kwd">double</span><span class="pln"> input1</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">double</span><span class="pln"> input2</span><span class="pun">){</span><span class="pln">

  </span><span class="kwd">return</span><span class="pln"> calcService</span><span class="pun">.</span><span class="pln">divide</span><span class="pun">(</span><span class="pln">input1</span><span class="pun">,</span><span class="pln"> input2</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span></pre>

<p>

让我们来测试MathApplication类,通过它注入CalculatorService作一个模拟。Mock将由EasyMock创建。</p>

MathApplicationTester.java

<pre class="prettyprint prettyprinted" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb>

<span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">easymock</span><span class="pun">.</span><span class="typ">EasyMock</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">easymock</span><span class="pun">.</span><span class="typ">EasyMockRunner</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">easymock</span><span class="pun">.</span><span class="typ">Mock</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">easymock</span><span class="pun">.</span><span class="typ">TestSubject</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="typ">Assert</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="typ">Before</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="typ">Test</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runner</span><span class="pun">.</span><span class="typ">RunWith</span><span class="pun">;</span><span class="pln">

</span><span class="com">//@RunWith attaches a runner with the test class to initialize the test data</span><span class="pln">

</span><span class="lit">@RunWith </span><span class="pun">(</span><span class="typ">EasyMockRunner</span><span class="pun">.</span><span class="kwd">class</span><span class="pun">)</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MathApplicationTester</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="com">//@TestSubject annotation is used to identify class which is going to use the</span><span class="pln">

</span><span class="com">//mock object</span><span class="pln">

</span><span class="lit">@TestSubject </span><span class="pln">

</span><span class="typ">MathApplication</span><span class="pln"> mathApplication </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">MathApplication</span><span class="pun">();</span><span class="pln">

</span><span class="com">//@Mock annotation is used to create the mock object to be injected</span><span class="pln">

</span><span class="lit">@Mock </span><span class="pln">

</span><span class="typ">CalculatorService</span><span class="pln"> calcService</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@Test </span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> testAdd</span><span class="pun">(){</span><span class="pln">

  </span><span class="com">//add the behavior of calc service to add two numbers</span><span class="pln">

  </span><span class="typ">EasyMock</span><span class="pun">.</span><span class="pln">expect</span><span class="pun">(</span><span class="pln">calcService</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="lit">10.0</span><span class="pun">,</span><span class="lit">20.0</span><span class="pun">)).</span><span class="pln">andReturn</span><span class="pun">(</span><span class="lit">30.00</span><span class="pun">);</span><span class="pln">



  </span><span class="com">//activate the mock</span><span class="pln">

  </span><span class="typ">EasyMock</span><span class="pun">.</span><span class="pln">replay</span><span class="pun">(</span><span class="pln">calcService</span><span class="pun">);</span><span class="pln">



  </span><span class="com">//test the add functionality</span><span class="pln">

  </span><span class="typ">Assert</span><span class="pun">.</span><span class="pln">assertEquals</span><span class="pun">(</span><span class="pln">mathApplication</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="lit">10.0</span><span class="pun">,</span><span class="pln"> </span><span class="lit">20.0</span><span class="pun">),</span><span class="lit">30.0</span><span class="pun">,</span><span class="lit">0</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span></pre>

<p>

创建一个Java类在文件夹 C:\ &gt; EasyMock_WORKSPACE 执行测试用例</p>

TestRunner.java

<pre class="prettyprint prettyprinted" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb>

<span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runner</span><span class="pun">.</span><span class="typ">JUnitCore</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runner</span><span class="pun">.</span><span class="typ">Result</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runner</span><span class="pun">.</span><span class="pln">notification</span><span class="pun">.</span><span class="typ">Failure</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestRunner</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="typ">Result</span><span class="pln"> result </span><span class="pun">=</span><span class="pln"> </span><span class="typ">JUnitCore</span><span class="pun">.</span><span class="pln">runClasses</span><span class="pun">(</span><span class="typ">MathApplicationTester</span><span class="pun">.</span><span class="kwd">class</span><span class="pun">);</span><span class="pln">

  </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Failure</span><span class="pln"> failure </span><span class="pun">:</span><span class="pln"> result</span><span class="pun">.</span><span class="pln">getFailures</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

     </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">failure</span><span class="pun">.</span><span class="pln">toString</span><span class="pun">());</span><span class="pln">

  </span><span class="pun">}</span><span class="pln">

  </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">result</span><span class="pun">.</span><span class="pln">wasSuccessful</span><span class="pun">());</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln"> </span></pre>

<h2>

验证结果</h2>

<p>

编译使用javac编译如下的类</p>

<pre class="result" courier new monospace margin-bottom: border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: font-size: overflow: auto color: rgb line-height: background-color:>

C:\EasyMock_WORKSPACE&gt;javac CalculatorService.java MathApplication.java MathApplicationTester.java TestRunner.java
</pre>

<p>

现在运行测试运行看结果</p>

<pre class="result" courier new monospace margin-bottom: border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: font-size: overflow: auto color: rgb line-height: background-color:>

C:\EasyMock_WORKSPACE&gt;java TestRunner
</pre>

<p>

验证输出</p>

<pre class="result" courier new monospace margin-bottom: border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: font-size: overflow: auto color: rgb line-height: background-color:>

true</pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="EasyMock第一应用程序"><i class="layui-icon"></i>
                            EasyMock第一应用程序</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:EasyMock添加行为"><i class="layui-icon"></i>
                            EasyMock添加行为</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复