Return-Path: X-Original-To: apmail-openwebbeans-commits-archive@www.apache.org Delivered-To: apmail-openwebbeans-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CD568109B2 for ; Sun, 19 Apr 2015 20:47:00 +0000 (UTC) Received: (qmail 17190 invoked by uid 500); 19 Apr 2015 20:47:00 -0000 Delivered-To: apmail-openwebbeans-commits-archive@openwebbeans.apache.org Received: (qmail 17163 invoked by uid 500); 19 Apr 2015 20:47:00 -0000 Mailing-List: contact commits-help@openwebbeans.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openwebbeans.apache.org Delivered-To: mailing list commits@openwebbeans.apache.org Received: (qmail 17151 invoked by uid 99); 19 Apr 2015 20:47:00 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Apr 2015 20:47:00 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id 962D2AC006E for ; Sun, 19 Apr 2015 20:47:00 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r948319 - in /websites/staging/openwebbeans/trunk/content: ./ owbinternalunittests.html Date: Sun, 19 Apr 2015 20:47:00 -0000 To: commits@openwebbeans.apache.org From: buildbot@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150419204700.962D2AC006E@hades.apache.org> Author: buildbot Date: Sun Apr 19 20:47:00 2015 New Revision: 948319 Log: Staging update by buildbot for openwebbeans Added: websites/staging/openwebbeans/trunk/content/owbinternalunittests.html Modified: 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 @@ -1674686 +1674695 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 2015 @@ -0,0 +1,166 @@ + + + + + + Apache OpenWebBeans + + + + + + + + + + + + + + + + +
+ + +
+

Testing strategies for unit tests inside OpenWebBeans

+

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.

+

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.

+

Good Practice

+

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 leverage +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.

+

Start small with plain tests

+

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 @PostConstruct) then go ahead and upgrade the test to be CDI aware.

+

CDI aware tests

+

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 org.apache.webbeans.test.AbstractUnitTest base class.

+

Lets look at how to write a unit test which e.g. tests a method invocation on a specific CDI bean.

+

The first thing we obviously need is the CDI bean which should get tested:

+
@RequestScoped
+public class BeanUnderTest
+{
+    public int meaningOfLife()
+    {
+        return 42;
+    }
+}
+
+ + +

And now let's write the unit test which calls this method:

+
public class MySimpleTest extends AbstractUnitTest
+{
+    @Test
+    public void testMeaningOfLife()
+    {
+        startContainer(BeanUnderTest.class);
+        BeanUnderTest instance = getInstance(BeanUnderTest.class);
+        Assert.assertnotNull(instance);
+        Assert.assertEquals(42, instance.meaningOfLife());
+    }
+}
+
+ + +

That's all! You don't need even need to manually shut down the container after the method. +If you have multiple beans to test then pass all of them as argument to startContainer(Class...);. +There are also startContainer variants which take a beans.xml. Also look at the other methods of AbstractUnitTest for more useful features.

+
+ +
+ +
+

+ asf_feather +

+

Copyright © 2008-2013 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.

+

OpenWebBeans, Apache and the Apache feather logo are trademarks of The Apache Software Foundation.

+
+ +
+ + + + + + + + + + + + + + + + + + + + + +