main website home
  • About this blog

    This blog features updates, opinions, and technical notes from Caucho engineers about Caucho products, the enterprise Java industry, and PHP. Caucho Technology is the creator of the Resin Application Server and the Quercus PHP in Java engine. A leader in Java performance since 1998, Caucho is a Sun JavaEE licensee with over 9000 customers worldwide.
  • Tags

    ajaxworld bam candi cdi cloud cluster comet configuration deploy devoxx eclipse ejb embedded flash flex google app engine hessian hmtp ioc java ee 6 javaone javazone jms messaging newsletter nyjug osgi php pomegranate quercus resin resin 4.0 REST servlet sfjug silicon valley code camp spring testing training tssjs watchdog webbeans web profile websockets wordpress
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
« Put Resin statistics graphs into anything using REST
Servlet 3.0 TCK - web-profile step 1 »

ResinBeanContainer: embedding CDI for testing

Since unit testing and test-driven development have become accepted as the basis for solid programming practices, we’ve had several requests to provide a lightweight Resin environment specifically tailored to test environments. As our first cut, we’ve created a ResinBeanContainer class, usable outside of Resin, but providing a Resin environment.

The ResinBeanConstainer provides the same environment as a Resin web-app, but without the servlets, HTTP or requests. It provides CDI injection, EJB lite support, database and JPA configuration.

Like Resin, the ResinBeanContainer scans the classpath for CDI beans.xml and EJB ejb-jar.xml files and populates the container with the found beans.

package example;

import com.caucho.resin.ResinBeanContainer;

class MyTestClass {
public void myTest()
{
  ResinBeanContainer beans = new ResinBeanContainer();
  beans.start();

  RequestContext request = beans.startRequest();
  try {
    MyBean bean = beans.getInstance(MyBean.class);

    bean.doStuff();
  } finally {
    request.close();
  }

  beans.close();
}

The ResinBeansContainer.start() call initializes the container, telling it to scan the classpath, and instantiate and start @Startup beans.

The ResinBeansContainer.startRequest() creates a new CDI @RequestScoped context for the current thread. The thread is now inside CDI and inside the container, basically running as if you were in a live Resin instance.

The ResinBeansContainer.getInstance(MyBean.class) creates a CDI-injected instance of the MyBean class. Assuming MyBean has @Inject methods, you’ll see the populated values.

And finally, the close() methods complete the request and close the ResinBeansContainer context. It’s critical to close both the request context and the container, to make sure they are properly garbage collected.

This entry was posted on Tuesday, January 19th, 2010 at 11:45 am and is filed under Engineering. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.


Caucho Technology is proudly powered by WordPress and Quercus®
Entries (RSS) and Comments (RSS).

  • HOME |
  • CONTACT US |
  • DOCUMENTATION |
  • BLOG |
  • WIKI 4 |
  • WIKI 3 |
  • Resin: Java Application Server
Copyright (c) 1998-2012 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.
resin® is a cloud optimized, java® application server that supports the java ee webprofile ®