openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject svn commit: r948319 - in /websites/staging/openwebbeans/trunk/content: ./ owbinternalunittests.html
Date Sun, 19 Apr 2015 20:47:00 GMT
Author: buildbot
Date: Sun Apr 19 20:47:00 2015
New Revision: 948319

Staging update by buildbot for openwebbeans

    websites/staging/openwebbeans/trunk/content/   (props changed)

Propchange: websites/staging/openwebbeans/trunk/content/
--- cms:source-revision (original)
+++ cms:source-revision Sun Apr 19 20:47:00 2015
@@ -1 +1 @@

Added: websites/staging/openwebbeans/trunk/content/owbinternalunittests.html
--- websites/staging/openwebbeans/trunk/content/owbinternalunittests.html (added)
+++ websites/staging/openwebbeans/trunk/content/owbinternalunittests.html Sun Apr 19 20:47:00
@@ -0,0 +1,166 @@
+<!DOCTYPE html>
+<html lang="en">
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    <meta charset="utf-8">
+    <title>Apache OpenWebBeans</title>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <meta name="description" content="">
+    <meta name="author" content="">
+    <!-- Le styles -->
+    <link href="./resources/css/bootstrap.css" rel="stylesheet">
+    <style type="text/css">
+        body {
+            padding-top: 60px;
+            padding-bottom: 40px;
+        }
+    </style>
+    <link href=""
+    <link href="./resources/css/main.css" rel="stylesheet">
+<div class="navbar navbar-fixed-top">
+    <div class="navbar-inner">
+        <div class="container">
+            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
+                <span class="icon-bar"></span>
+                <span class="icon-bar"></span>
+                <span class="icon-bar"></span>
+            </a>
+            <a class="brand" href="index.html"><img src="./resources/images/logo_dbg-small.png"
+                                                    alt="owb_logo_small"/></a>
+            <div class="nav-collapse">
+                <ul class="nav">
+                    <li class="active"><a href="./index.html">Home</a></li>
+                    <li><a href="./documentation.html">Documentation</a></li>
+                    <li><a href="./source.html">Source</a></li>
+                    <li><a href="./download.html">Download</a></li>
+                    <li><a href="./community.html">Community</a></li>
+                    <!-- <li><a href="./news.html">News</a></li>
+                    <li class="dropdown">
+                        <a class="dropdown-toggle"
+                           data-toggle="dropdown"
+                           href="#">
+                            Misc
+                            <b class="caret"></b>
+                        </a>
+                        <ul class="dropdown-menu">
+                            <li><a href="">Apache Home</a></li>
+                            <li><a href="./misc/contact.html">Contact</a></li>
+                            <li><a href="./misc/legal.html">Legal</a></li>
+                            <li><a href="">Sponsorship</a></li>
+                            <li><a href="">Thanks</a></li>
+                            <!-- <li class="divider"/> -->
+                        </ul>
+                    </li>
+                </ul>
+            </div>
+            <!--/.nav-collapse -->
+            <form id="search-form" action="" method="get"
class="navbar-search pull-right">
+                <input value="" name="sitesearch" type="hidden"/>
+                <input class="search-query" name="q" id="query" type="text"/>
+            </form>
+            <script type="text/javascript" src=""></script>
+        </div>
+    </div>
+<div class="container">
+        <div id="OwbContent_" class="wiki-content">
+<h1 id="testing-strategies-for-unit-tests-inside-openwebbeans">Testing strategies for
unit tests inside OpenWebBeans</h1>
+<p>One could argue that unit tests are harder to write when your instances are managed
by a container. 
+However this is only true if the booting of said container is uncharted territory. 
+But lets assume the container control is taken cared of handily. Well then it can be quite
the breath of fresh air to test code 
+that leverage dependency injection.</p>
+<p>Here comes the good news. Testing OpenWebBeans and CDI in general (and actually
many other Java EE frameworks) 
+is in a good state as of today and testing frameworks are reaching a pretty decent level
of maturity. Stay with us and 
+we will compare the pros and cons with 
+the different strategies. </p>
+<h3 id="good-practice">Good Practice</h3>
+<p>We won't go in to detail on how to properly use unit tests or integration tests
in your project. However so called "Whitebox Testing" is 
+discouraged. Using whitebox testing is often critizied regardless but with frameworks that
+proxies it's simply not something you should attempt. Any reflection trick would likely miss
the mark and modify the proxy.
+Instead focus on functional tests and structure your code with high cohesion so that testing
the public methods get's the job done.
+Remember to add beans.xml and other resources to your to your test path.</p>
+<h3 id="start-small-with-plain-tests">Start small with plain tests</h3>
+<p>Testing code that leverage CDI does not differ much from using CDI in your project.
You should start with just a plain pojo 
+in your project and likewise a plain unit test. Only when you need context should you upgrade
the pojo to a CDI managed instance.
+Still this does not mean you automatically need something more then plain unit test. But
when you need the container to act on your
+instances (for example to trigger <code>@PostConstruct</code>) then go ahead
and upgrade the test to be CDI aware.</p>
+<h3 id="cdi-aware-tests">CDI aware tests</h3>
+<p>In OpenWebBeans we use JUnit as testing framework. For not having to deal with the
container details each time you can
+simply write a JUnit test which extends the <code>org.apache.webbeans.test.AbstractUnitTest</code>
base class.</p>
+<p>Lets look at how to write a unit test which e.g. tests a method invocation on a
specific CDI bean.</p>
+<p>The first thing we obviously need is the CDI bean which should get tested:</p>
+<div class="codehilite"><pre><span class="p">@</span><span class="n">RequestScoped</span>
+<span class="n">public</span> <span class="n">class</span> <span
+<span class="p">{</span>
+    <span class="n">public</span> <span class="n">int</span> <span
class="n">meaningOfLife</span><span class="p">()</span>
+    <span class="p">{</span>
+        <span class="k">return</span> 42<span class="p">;</span>
+    <span class="p">}</span>
+<span class="p">}</span>
+<p>And now let's write the unit test which calls this method:</p>
+<div class="codehilite"><pre><span class="n">public</span> <span
class="n">class</span> <span class="n">MySimpleTest</span> <span class="n">extends</span>
<span class="n">AbstractUnitTest</span>
+<span class="p">{</span>
+    <span class="p">@</span><span class="n">Test</span>
+    <span class="n">public</span> <span class="n">void</span> <span
class="n">testMeaningOfLife</span><span class="p">()</span>
+    <span class="p">{</span>
+        <span class="n">startContainer</span><span class="p">(</span><span
class="n">BeanUnderTest</span><span class="p">.</span><span class="n">class</span><span
+        <span class="n">BeanUnderTest</span> <span class="n">instance</span>
<span class="p">=</span> <span class="n">getInstance</span><span
class="p">(</span><span class="n">BeanUnderTest</span><span class="p">.</span><span
class="n">class</span><span class="p">);</span>
+        <span class="n">Assert</span><span class="p">.</span><span
class="n">assertnotNull</span><span class="p">(</span><span class="n">instance</span><span
+        <span class="n">Assert</span><span class="p">.</span><span
class="n">assertEquals</span><span class="p">(</span>42<span class="p">,</span>
<span class="n">instance</span><span class="p">.</span><span class="n">meaningOfLife</span><span
+    <span class="p">}</span>
+<span class="p">}</span>
+<p>That's all! You don't need even need to manually shut down the container after the
+If you have multiple beans to test then pass all of them as argument to <code>startContainer(Class...);</code>.
+There are also startContainer variants which take a beans.xml. Also look at the other methods
of <code>AbstractUnitTest</code> for more useful features.</p>
+        </div>
+        <hr>
+        <footer>
+            <p>
+                <a class="brand" href=""><img src=""
height="55px" alt="asf_feather"/></a>
+            </p>
+            <p>Copyright &copy; 2008-2013 The Apache Software Foundation, Licensed
under the Apache License, Version 2.0.</p>
+            <p>OpenWebBeans, Apache and the Apache feather logo are trademarks of The
Apache Software Foundation.</p>
+        </footer>
+    </div> <!-- /container -->
+    <!-- Javascript
+    ================================================== -->
+    <!-- Placed at the end of the document so the pages load faster -->
+    <script src=".//resources/js/jquery.js"></script>
+    <script src=".//resources/js/bootstrap-transition.js"></script>
+    <script src=".//resources/js/bootstrap-alert.js"></script>
+    <script src=".//resources/js/bootstrap-modal.js"></script>
+    <script src=".//resources/js/bootstrap-dropdown.js"></script>
+    <script src=".//resources/js/bootstrap-scrollspy.js"></script>
+    <script src=".//resources/js/bootstrap-tab.js"></script>
+    <script src=".//resources/js/bootstrap-tooltip.js"></script>
+    <script src=".//resources/js/bootstrap-popover.js"></script>
+    <script src=".//resources/js/bootstrap-button.js"></script>
+    <script src=".//resources/js/bootstrap-collapse.js"></script>
+    <script src=".//resources/js/bootstrap-carousel.js"></script>
+    <script src=".//resources/js/bootstrap-typeahead.js"></script>

View raw message