deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r908919 - in /websites/staging/deltaspike/trunk/content: ./ test-control.html
Date Thu, 15 May 2014 15:36:19 GMT
Author: buildbot
Date: Thu May 15 15:36:19 2014
New Revision: 908919

Log:
Staging update by buildbot for deltaspike

Modified:
    websites/staging/deltaspike/trunk/content/   (props changed)
    websites/staging/deltaspike/trunk/content/test-control.html

Propchange: websites/staging/deltaspike/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Thu May 15 15:36:19 2014
@@ -1 +1 @@
-1594336
+1594951

Modified: websites/staging/deltaspike/trunk/content/test-control.html
==============================================================================
--- websites/staging/deltaspike/trunk/content/test-control.html (original)
+++ websites/staging/deltaspike/trunk/content/test-control.html Thu May 15 15:36:19 2014
@@ -93,6 +93,7 @@
 <li><a href="#optional-config">Optional Config</a></li>
 <li><a href="#hints">Hints</a></li>
 <li><a href="#optional-integrations">Optional Integrations</a><ul>
+<li><a href="#mock-frameworks">Mock Frameworks</a></li>
 <li><a href="#jsf-via-myfaces-test">JSF (via MyFaces-Test)</a></li>
 </ul>
 </li>
@@ -213,6 +214,152 @@
 <p>Don't forget to add a beans.xml in the test-module (e.g. src/test/resources/META-INF/beans.xml).</p>
 <p>If you are using OpenWebBeans as CDI implementation and you need to test EJBs as
well, you can use deltaspike-cdictrl-openejb + org.apache.openejb:openejb-core (instead of
deltaspike-cdictrl-owb).</p>
 <h1 id="optional-integrations">Optional Integrations</h1>
+<h2 id="mock-frameworks">Mock Frameworks</h2>
+<p>With v0.8+ it's possible to mock CDI-Beans. Usually @Exclude (+ project-stage) is
enough, however, for some cases mocked beans might be easier. Therefore it's possible to create
(mock-)instances manually or via a mocking framework and add them e.g. via <code>DynamicMockManager</code>.</p>
+<p>If you need dependency-injection in the mocked instances, you can use <code>BeanProvider.injectFields(myMockedBean);</code>.</p>
+<div class="codehilite"><pre><span class="nd">@RunWith</span><span
class="o">(</span><span class="n">CdiTestRunner</span><span class="o">.</span><span
class="na">class</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">MockedRequestScopedBeanTest</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">RequestScopedBean</span>
<span class="n">requestScopedBean</span><span class="o">;</span>
+
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">DynamicMockManager</span>
<span class="n">mockManager</span><span class="o">;</span>
+
+    <span class="nd">@Test</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">manualMock</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="n">mockManager</span><span class="o">.</span><span
class="na">addMock</span><span class="o">(</span><span class="k">new</span>
<span class="n">RequestScopedBean</span><span class="o">()</span>
<span class="o">{</span>
+            <span class="nd">@Override</span>
+            <span class="kd">public</span> <span class="kt">int</span>
<span class="nf">getCount</span><span class="o">()</span>
+            <span class="o">{</span>
+                <span class="k">return</span> <span class="mi">7</span><span
class="o">;</span>
+            <span class="o">}</span>
+        <span class="o">});</span>
+
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">7</span><span
class="o">,</span> <span class="n">requestScopedBean</span><span class="o">.</span><span
class="na">getCount</span><span class="o">());</span>
+        <span class="n">requestScopedBean</span><span class="o">.</span><span
class="na">increaseCount</span><span class="o">();</span>
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">7</span><span
class="o">,</span> <span class="n">requestScopedBean</span><span class="o">.</span><span
class="na">getCount</span><span class="o">());</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+
+<span class="nd">@RequestScoped</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">RequestScopedBean</span>
+<span class="o">{</span>
+    <span class="kd">private</span> <span class="kt">int</span> <span
class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span
class="o">;</span>
+
+    <span class="kd">public</span> <span class="kt">int</span> <span
class="nf">getCount</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="n">count</span><span
class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">increaseCount</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">this</span><span class="o">.</span><span
class="na">count</span><span class="o">++;</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Using a mocking framework makes no difference for adding the mock. E.g. via Mockito:</p>
+<div class="codehilite"><pre><span class="nd">@RunWith</span><span
class="o">(</span><span class="n">CdiTestRunner</span><span class="o">.</span><span
class="na">class</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">MockitoMockedRequestScopedBeanTest</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">RequestScopedBean</span>
<span class="n">requestScopedBean</span><span class="o">;</span>
+
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">DynamicMockManager</span>
<span class="n">mockManager</span><span class="o">;</span>
+
+    <span class="nd">@Test</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">mockitoMockAsCdiBean</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="n">RequestScopedBean</span> <span class="n">mockedRequestScopedBean</span>
<span class="o">=</span> <span class="n">mock</span><span class="o">(</span><span
class="n">RequestScopedBean</span><span class="o">.</span><span class="na">class</span><span
class="o">);</span>
+        <span class="n">when</span><span class="o">(</span><span
class="n">mockedRequestScopedBean</span><span class="o">.</span><span
class="na">getCount</span><span class="o">()).</span><span class="na">thenReturn</span><span
class="o">(</span><span class="mi">7</span><span class="o">);</span>
+        <span class="n">mockManager</span><span class="o">.</span><span
class="na">addMock</span><span class="o">(</span><span class="n">mockedRequestScopedBean</span><span
class="o">);</span>
+
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">7</span><span
class="o">,</span> <span class="n">requestScopedBean</span><span class="o">.</span><span
class="na">getCount</span><span class="o">());</span>
+        <span class="n">requestScopedBean</span><span class="o">.</span><span
class="na">increaseCount</span><span class="o">();</span>
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">7</span><span
class="o">,</span> <span class="n">requestScopedBean</span><span class="o">.</span><span
class="na">getCount</span><span class="o">());</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Since CDI implementations like OpenWebBeans use a lot of optimizations, it's required
to handle mocks for application-scoped beans differently - e.g.:
+However, <code>ApplicationMockManager</code> can be used for adding all mocks,
if they should be active for the lifetime of the CDI-container.</p>
+<div class="codehilite"><pre><span class="nd">@RunWith</span><span
class="o">(</span><span class="n">CdiTestRunner</span><span class="o">.</span><span
class="na">class</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">MockedApplicationScopedBeanTest</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">ApplicationScopedBean</span>
<span class="n">applicationScopedBean</span><span class="o">;</span>
+
+    <span class="nd">@BeforeClass</span>
+    <span class="kd">public</span> <span class="kd">static</span>
<span class="kt">void</span> <span class="nf">init</span><span
class="o">()</span>
+    <span class="o">{</span>
+        <span class="n">ApplicationMockManager</span> <span class="n">applicationMockManager</span>
<span class="o">=</span> <span class="n">BeanProvider</span><span
class="o">.</span><span class="na">getContextualReference</span><span
class="o">(</span><span class="n">ApplicationMockManager</span><span
class="o">.</span><span class="na">class</span><span class="o">);</span>
+        <span class="n">applicationMockManager</span><span class="o">.</span><span
class="na">addMock</span><span class="o">(</span><span class="k">new</span>
<span class="n">MockedApplicationScopedBean</span><span class="o">());</span>
+    <span class="o">}</span>
+
+    <span class="nd">@Test</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">manualMock</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">14</span><span
class="o">,</span> <span class="n">applicationScopedBean</span><span
class="o">.</span><span class="na">getCount</span><span class="o">());</span>
+        <span class="n">applicationScopedBean</span><span class="o">.</span><span
class="na">increaseCount</span><span class="o">();</span>
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">14</span><span
class="o">,</span> <span class="n">applicationScopedBean</span><span
class="o">.</span><span class="na">getCount</span><span class="o">());</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+
+<span class="nd">@ApplicationScoped</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">ApplicationScopedBean</span>
+<span class="o">{</span>
+    <span class="kd">private</span> <span class="kt">int</span> <span
class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span
class="o">;</span>
+
+    <span class="kd">public</span> <span class="kt">int</span> <span
class="nf">getCount</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">return</span> <span class="n">count</span><span
class="o">;</span>
+    <span class="o">}</span>
+
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">increaseCount</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="k">this</span><span class="o">.</span><span
class="na">count</span><span class="o">++;</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>It's also possible to mock qualified beans. Just add the literal-instance(s) as
additional parameter(s) - e.g.:</p>
+<div class="codehilite"><pre><span class="nd">@RunWith</span><span
class="o">(</span><span class="n">CdiTestRunner</span><span class="o">.</span><span
class="na">class</span><span class="o">)</span>
+<span class="kd">public</span> <span class="kd">class</span> <span
class="nc">MockedQualifiedBeanTest</span>
+<span class="o">{</span>
+    <span class="nd">@Inject</span>
+    <span class="nd">@MyQualifier</span>
+    <span class="kd">private</span> <span class="n">QualifiedBean</span>
<span class="n">qualifiedBean</span><span class="o">;</span>
+
+    <span class="nd">@Inject</span>
+    <span class="kd">private</span> <span class="n">DynamicMockManager</span>
<span class="n">mockManager</span><span class="o">;</span>
+
+    <span class="nd">@Test</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span
class="nf">manualMockWithQualifier</span><span class="o">()</span>
+    <span class="o">{</span>
+        <span class="n">mockManager</span><span class="o">.</span><span
class="na">addMock</span><span class="o">(</span><span class="k">new</span>
<span class="n">QualifiedBean</span><span class="o">()</span> <span
class="o">{</span>
+            <span class="nd">@Override</span>
+            <span class="kd">public</span> <span class="kt">int</span>
<span class="nf">getCount</span><span class="o">()</span>
+            <span class="o">{</span>
+                <span class="k">return</span> <span class="mi">21</span><span
class="o">;</span>
+            <span class="o">}</span>
+        <span class="o">},</span> <span class="n">AnnotationInstanceProvider</span><span
class="o">.</span><span class="na">of</span><span class="o">(</span><span
class="n">MyQualifier</span><span class="o">.</span><span class="na">class</span><span
class="o">));</span>
+
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">21</span><span
class="o">,</span> <span class="n">qualifiedBean</span><span class="o">.</span><span
class="na">getCount</span><span class="o">());</span>
+        <span class="n">qualifiedBean</span><span class="o">.</span><span
class="na">increaseCount</span><span class="o">();</span>
+        <span class="n">Assert</span><span class="o">.</span><span
class="na">assertEquals</span><span class="o">(</span><span class="mi">21</span><span
class="o">,</span> <span class="n">qualifiedBean</span><span class="o">.</span><span
class="na">getCount</span><span class="o">());</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>In some cases it's needed to use <code>@javax.enterprise.inject.Typed</code>.
Mocking such typed beans can result in an <code>AmbiguousResolutionException</code>.
+Therefore it's needed to exclude the mocked implementation via <code>@Exclude</code>
or <code>@Typed()</code> (or a parametrized constructor) and specify the target-type
via <code>@TypedMock</code>.</p>
 <h2 id="jsf-via-myfaces-test">JSF (via MyFaces-Test)</h2>
 <p>add:</p>
 <p>org.apache.deltaspike.testcontrol.impl.jsf.MockedJsf2TestContainer



Mime
View raw message